三角形

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,保证没有重合的点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
inline int read() {
    int res=0;char c=getchar();bool f=0;
    while(!isdigit(c)) {if(c=='-')f=1;c=getchar();}
    while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar();
    return f?-res:res;
}
#define ll long long
int n;
ll C[3005][3005];
double slope[3005*3000];
int cnt, num;
int x[3005], y[3005];
ll ans;

int main()
{
    freopen("triangle.in", "r", stdin);
    freopen("triangle.out", "w", stdout);
    n = read();
    C[0][0] = 1;
    for (register int i = 1 ; i <= n ; i ++)
    {
        C[i][0] = 1;
        for (register int j = 1 ; j <= i ; j ++)
            C[i][j] = C[i-1][j-1] + C[i-1][j];
    }
    ans = C[n][3];
    for(register int i = 1 ; i <= n ; i ++) x[i] = read(), y[i] = read();
    for (register int i = 1 ; i <= n - 1 ; i ++)
    {
        cnt = 0, num = 0;
        for (register int j = i + 1 ; j <= n ; j ++)
        {
            if (x[i] == x[j]) num++;
            else slope[++cnt] = (double)((y[i]-y[j]))/(double)((x[i]-x[j]));
        }
        sort(slope + 1, slope + 1 + cnt);
        int sum = 1;
        for (register int j = 2 ; j <= cnt ; j ++)
        {
            if (slope[j] == slope[j-1]) sum++;
            else {
                ans -= C[sum][2];
                sum = 1;
            }
        }
        ans -= C[sum][2];
        ans -= C[num][2];
    }    
    printf("%lld
", ans);
    fclose(stdin), fclose(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/BriMon/p/9433848.html