链接:https://ac.nowcoder.com/acm/contest/997/K
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤ Yi ≤ 1,000).
Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.
Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.
输入描述:
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 describes lattice point i with two space-separated integers: Xi and Yi.
输出描述:
* Line 1: A single integer representing the maximal number of lines Bessie can draw, no two of which are parallel.
示例1
输入
4 -1 1 -2 0 0 0 1 1
输出
4
说明
Bessie can draw lines of the following four slopes: -1, 0, 1/3, and 1.
排个序再特判一下就出来了
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 typedef long long ll; 7 typedef char ch; 8 typedef double db; 9 10 void quick_sort(double q[],int l,int r) 11 { 12 if(l >= r) return; 13 14 int i=l-1,j=r+1; 15 double x=q[(l + r) >> 1]; 16 while(i<j){ 17 do i ++; while (q[i] < x); 18 do j --; while (q[j] > x); 19 if(i < j) swap(q[i] , q[j]); 20 } 21 quick_sort(q, l, j); 22 quick_sort(q, j + 1, r); 23 } 24 25 int main() 26 { 27 double x[300] = {0}; 28 double y[300] = {0}; 29 double a[50000] = {0}; 30 int marker = 0; 31 int n, score = 0; 32 33 cin >> n; 34 for(int j = 0;j<n;j++) 35 { 36 scanf("%lf %lf",&x[j],&y[j]); 37 } 38 int L = 0; 39 for(int j = 0;j<n-1;j++) 40 { 41 for(int k = j+1;k<n;k++) 42 { 43 if(x[j] == x[k]) 44 { 45 marker = 1; 46 } 47 else 48 { 49 a[L++] = double(y[k]-y[j])/(x[k]-x[j]); 50 } 51 } 52 } 53 //cout<<score<<endl; 54 quick_sort(a,0,L-1); 55 56 /*for(int j = 0;j<L;j++) 57 { 58 cout<<a[j]<<" "; 59 }*/ 60 if(n > 2) 61 { 62 for(int j = 0;j<L-1 ;j++) 63 { 64 if(a[j]!=a[j+1]) score++; 65 } 66 score++; 67 } 68 if(marker) 69 { 70 score+=1; 71 // cout<<'!'; 72 } 73 if(L == 0) score--; 74 cout<<score<<endl; 75 return 0; 76 }
之前因为有个暴力+1
所以当所有点在一条直线上且斜率不存在的时候会重复计数
这个故事告诉我们不能随便暴力+
错误代码(供自己反省)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdlib> 3 using namespace std; 4 5 typedef long long ll; 6 typedef char ch; 7 typedef double db; 8 9 int cmp(const void * a, const void * b) 10 { 11 return((*(double*)a-*(double*)b>0)?1:-1); 12 } 13 14 double x[10005] = {0}; 15 double y[10005] = {0}; 16 double l[10005] = {0}; 17 18 int main() 19 { 20 int marker = 0; 21 int n = 0 , score = 0; 22 23 cin >> n; 24 for(int i = 0;i<n;i++) 25 { 26 cin>>x[i]>>y[i]; 27 } 28 int i = 0; 29 for(int j = 1;j<=n-1;j++) 30 { 31 for(int k = j+1;k<n;k++) 32 { 33 if(x[j] == x[k]) 34 { 35 marker = 1; 36 } 37 else 38 { 39 l[i] = double(y[k]-y[j])/(x[k]-x[j]); 40 i++; 41 } 42 } 43 } 44 //cout<<score<<endl; 45 qsort(l,i,sizeof(l[0]),cmp); 46 /* 47 for(int j = 0;j<i;j++) 48 { 49 cout<<l[j]<<" "; 50 } 51 */ 52 if(n > 2) 53 { 54 for(int j = 0;j<i-1 ;j++) 55 { 56 if(l[j]!=l[j+1])score+=1; 57 } 58 score++; 59 } 60 if(marker) 61 { 62 score+=1; 63 //cout<<'!'; 64 } 65 cout<<score<<endl; 66 return 0; 67 }