zoukankan      html  css  js  c++  java
  • JZOJ 3493

    Description

    平面上有n个点,求出用这些点可以构成的三角形数。

    Input

    第一行一个整数n。

    接下来n行,每行两个整数,表示点的坐标。

    Output

    输出仅一个整数,表示所求答案。

    Sample Input

    5

    0 0

    1 1

    1 -1

    -1 -1

    -1 1

    Sample Output

    8

    Data Constraint

    对于50%的数据,n<=300。

    对于100%的数据,n<=3000,坐标的绝对值不超过10^4,保证没有重合的点。


    对于每个点求一其他点相对当前点的斜率,排序

    共线的点就挨在一起了,注意特判斜率不存在的情况

    容斥一波就好了

    不过在减去三点共线的三角形时,注意要用 C(cnt, 2),如果是 C(cnt + 1, 3) 的话,会忽略当前点,这样就不知道多算了多少了


    代码:

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cctype>
    #include<cmath>
    using namespace std;
    
    typedef long long ll;
    const int MAXN = 3005;
    const double eps = 1e-10;
    
    int n;
    ll ans, decr;
    pair<int, int> pos[MAXN];
    double k[MAXN];
    
    int main() {
    	freopen("triangle.in", "r", stdin);
    	freopen("triangle.out", "w", stdout);
    	scanf("%d", &n);
    	ans = (((ll)n * (ll)(n - 1) * (ll)(n - 2)) / 6ll);
    	for(int i = 1; i <= n; ++i) scanf("%d%d", &pos[i].first, &pos[i].second);
    	for(int i = 1; i <= n; ++i) {
    		ll cnt0 = 0ll, tmp = 0ll;
    		int ptr = 0;
    		for(int j = i + 1; j <= n; ++j) {
    			if(pos[i].first == pos[j].first) ++cnt0;
    			else k[++ptr] = (double)(pos[i].second - pos[j].second) / (double)(pos[i].first - pos[j].first);
    		}
    		decr += ((cnt0 * (cnt0 - 1ll)) >> 1);
    		sort(k + 1, k + ptr + 1);
    		k[0] = 12.345678;
    		for(int j = 1; j <= ptr; ++j) {
    			if(fabs(k[j] - k[j - 1]) > eps) {
    				decr += ((tmp * (tmp - 1ll)) >> 1);
    				tmp = 1ll;
    			}
    			else ++tmp;
    		}
    		decr += ((tmp * (tmp - 1ll)) >> 1);
    	}
    	ans -= decr;
    	printf("%lld
    ", ans);
    	return 0;
    }
    

      

    禁止诸如开发者知识库/布布扣/码迷/学步园/马开东等 copy 他人博文乃至博客的网站转载 ,用户转载请注明出处:https://www.cnblogs.com/xcysblog/
  • 相关阅读:
    3月4号—3月20号的计划
    Codeforces Round #344 (Div. 2) D. Messenger kmp水题
    Codeforces Round #344 (Div. 2) C. Report 水题
    整数三分(模板)
    Codeforces Round #344 (Div. 2) E. Product Sum 三分
    hdu3276 Graph and Queries 离线+treap
    bzoj1588: [HNOI2002]营业额统计 treap
    hdu5002 tree LCT
    bzoj2594 [Wc2006]水管局长数据加强版 离线+LCT维护边权
    bzoj2002 弹飞绵羊 LCT
  • 原文地址:https://www.cnblogs.com/xcysblog/p/9446144.html
Copyright © 2011-2022 走看看