Codeforces Round #308 (Div. 2) D. Vanya and Triangles (判断三角形数量)

D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane. 

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Examples
input
4
0 0
1 1
2 0
2 2
output
3
input
3
0 0
1 1
2 0
output
1
input
1
1 1
output
0
Note

Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0)(0, 0) - (2, 2) - (2, 0)(1, 1) - (2, 2) - (2, 0).

Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).

Note to the third sample test. A single point doesn't form a single triangle.

题意:

给出n个点,求将n个点相互连线之后,可组成多少个三角形。

分析:

本题时间给的太宽裕,n^3的暴力枚举加快速判断是否能形成三角形就可以解决,不过每个三角形都计算过3次,所以最终结果需要除以3。

更快速的方法是n^2的逐个按点枚举,将斜率存储下来。若该斜率大于1的话,证明该直线上存在2个以上的点,通过计算该斜率出现多少次,即可知道重复计算了哪些三角形。而n个点最多可形成n*(n-1)*(n-2)/3!个三角形,将重复计算的三角形除去即可。为了防止精度问题,将斜率的分子分母约分后,分别存储即可,本题采用了分子+分母*1000,作为该斜率的存储。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=2015;
const int mod=1e9+7;

struct node {
    int x,y;
}p[N];

int x[N*100];
long long sum=0;

int gcd(int a,int b) {
    return b==0?a:gcd(b, a%b);
}

bool cmp(node a,node b) {
    return a.x>b.x;
}

int solve(int i,int j) {
    int xx=p[i].x-p[j].x;
    int yy=p[i].y-p[j].y;
    if (xx<0) {
        xx*=-1;
        yy*=-1;
    }
    if (xx==0) {
        yy=abs(yy);
    }
    int t=gcd(xx, abs(yy));
    xx/=t;
    yy/=t;
    yy+=200;
    
    return xx*1000+yy;
}

int main() {
    int n;
    while (cin>>n) {
        for (int i=0; i<n; i++) {
            scanf("%d %d",&p[i].x,&p[i].y);
        }
        sort(p, p+n, cmp);
        sum=(long long)n*(n-1)*(n-2);
        long long temp=0;
        for (int i=0; i<n; i++) {
            temp=0;
            memset(x, 0, sizeof(x));
            for (int j=0; j<n; j++) {
                if (i==j) continue;
                x[solve(i, j)]++;
            }
            
            for (int j=0; j<n; j++) {
                if (i==j) continue;
                int flag=x[solve(i, j)]+1;
                x[solve(i, j)]=0;
                if (flag>2) {
                    temp+=(flag-1)*(flag-2);
                }
            }
            sum-=temp;
        }
        sum/=6;
        cout<<sum<<endl;
    }
    return 0;
}


你可能感兴趣的:(Codeforces Round #308 (Div. 2) D. Vanya and Triangles (判断三角形数量))