Codeforces Round #308 (Div. 2) D. Vanya and Triangles,
D. Vanya and Trianglestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard 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.
Sample test(s)input
40 01 12 02 2
output
3
input
30 01 12 0
output
1
input
11 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個點所能組成的三角形的個數,我們知道,若都能組成三角形,則總個數為c(n,3)個,現在目標就是求出不能組成三角形的個數,則
可以這樣求,以一個點為起點,其它點與這個點的斜率儲存在map中,則對於每個則有c(m,2)個不能組成三角形,因為重複算了三次,最後要除以3,
還要用long long 儲存結果,儲存斜率可以用分數a/b,這裡a,b互質,分數可以用a*N+b一個整數儲存起來,就沒有誤差了!
#define INF9000000000000000000#define EPS(double)1e-9#define mod1000000007#define PI3.14159265358979//*******************************************************************************/#endif#define N 2005#define MOD 1000000007int n;struct node{ int x,y; node(int xx,int yy){ x = xx,y = yy; } node(){ x = 0;y=0; }};node nodes[N];map<int,int> mymaps;map<int,int>::iterator it;int get(int a,int b){//a/b if(a == 0) return 0; if(b == 0) return N * N + N; int sa = a>=0?1:-1,sb= b>=0?1:-1; sa = sa * sb;sb = 1; a = abs(a);b=abs(b); int g = gcd(a,b); a/=g;b/=g; return sa * a * N + sa * b;}int main(){ //printf("%d",get(-1,2)); while (S(n) != EOF) { FI(n){ S2(nodes[i].x,nodes[i].y); } long long ans = (long long)n*((long long)n-1)*((long long)n-2)/6,anst = 0; FI(n){ mymaps.clear(); FJ(n){ if(i != j){ mymaps[get(nodes[j].y - nodes[i].y,nodes[j].x - nodes[i].x)]++; } } for(it = mymaps.begin();it!=mymaps.end();it++){ int num = it->second; anst += num>=2? num*(num-1)/2:0; } } cout<<ans - anst/3<<endl; } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。