Codeforces Round #431 (Div. 2) B. Tell Your World
题面:
Connect the countless points with lines, till we reach the faraway yonder.
There are n points on a coordinate plane, the i-th of which being (i, yi).
Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.
The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise.
You can print each letter in any case (upper or lower).
5
7 5 8 6 9
Yes
5
-1 -2 0 0 -5
No
5
5 4 3 2 1
No
5
1000000000 0 0 0 0
Yes
In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.
In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.
In the third example, it's impossible to satisfy both requirements at the same time.
思路:
因为一条直线最少过一个点,另一条直线则至少过n-1个点。
所以直接枚举那过两个点的直线即可,这样是n*n的做法。
但是其实没必要枚举那么多直线,只需考虑前三条直线即可。
对于三个点1,2,3
判断12在同一直线
23在同一直线
以上有一个符合情况就可以。
但是要特判掉所有点都在同一直线的情况
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n,vis[K]; 15 struct Point 16 { 17 LL x,y; 18 }pt[K],pp[K]; 19 LL dc(Point &ta,Point &tb,Point &tc) 20 { 21 return (tb.x-ta.x)*(tc.x-ta.x)+(tb.y-ta.y)*(tc.y-ta.y); 22 } 23 LL cc(Point &ta,Point &tb,Point &tc) 24 { 25 return (tb.x-ta.x)*(tc.y-ta.y)-(tc.x-ta.x)*(tb.y-ta.y); 26 } 27 bool check(void) 28 { 29 int cnt=0; 30 for(int i=3;i<=n;i++) 31 if(cc(pt[1],pt[2],pt[i])!=0) 32 pp[++cnt]=pt[i]; 33 for(int i=3;i<=cnt;i++) 34 if(cc(pp[1],pp[2],pp[i])!=0) 35 return 0; 36 Point ta,tb,tc; 37 ta.x=pt[2].x-pt[1].x,ta.y=pt[2].y-pt[1].y; 38 tb.x=pp[2].x-pp[1].x,tb.y=pp[2].y-pp[1].y; 39 tc.x=tc.y=0; 40 return cnt<2||cc(tc,ta,tb)==0; 41 } 42 int main(void) 43 { 44 int ff=0;cin>>n; 45 for(int i=1;i<=n;i++) 46 scanf("%lld",&pt[i].y),pt[i].x=i; 47 for(int i=3;i<=n&&!ff;i++) 48 if(cc(pt[i-2],pt[i-1],pt[i])!=0) 49 ff=1; 50 if(!ff) {printf("NO\n");return 0;} 51 if(check()) 52 {printf("YES\n");return 0;} 53 swap(pt[1],pt[3]); 54 if(check()) 55 {printf("YES\n");return 0;} 56 swap(pt[2],pt[3]); 57 if(check()) 58 {printf("YES\n");return 0;} 59 printf("NO\n"); 60 return 0; 61 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。