[ABC422E]Colinear题解
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 450 points
Problem Statement
There are N points on a two-dimensional plane. N is odd. The i-th point is at (xi,yi). All point coordinates are distinct.
Determine whether there exists a line passing through more than half of the N points, and if so, output it.
For any input satisfying the constraints, if a line satisfying the condition exists, it can be expressed as ax+by+c=0 using integers a,b,c with −1018≤a,b,c≤1018 (where (a,b,c)=(0,0,0)). Output these a,b,c.
讯飞听见 翻译
###问题陈述
二维平面上有 N 个点, N 为奇数。第 i 点位于 (xi,yi) 。
所有点的坐标都是不同的。
判断是否存在通过一半以上 N 点的直线,如果存在,则输出。
对于满足约束的任何输入,如果存在满足条件的线,它可以使用整数 a,b,c 和 −1018≤a,b,c≤1018 (其中 (a,b,c)=(0,0,0) )表示为 ax+by+c=0 . 输出这些 a,b,c 。
Constraints
- 3≤N≤5×105
- N is odd.
- −108≤xi≤108
- −108≤yi≤108
- If i=j, then (xi,yi)=(xj,yj).
- All input values are integers.
讯飞听见 翻译
###约束
-
3≤N≤5×105
-
N 为奇数。
-
−108≤xi≤108
-
−108≤yi≤108
-如果 i=j ,则 (xi,yi)=(xj,yj) 。
-所有输入值均为整数。
Input
The input is given from Standard Input in the following format:
N x1 y1 x2 y2 ⋮ xN yN
讯飞听见 翻译
###输入
输入来自标准输入,格式如下:
N
x1 y1
x2 y2
⋮
xN yN
Output
If no line satisfying the condition exists, output No.
If a line satisfying the condition exists, output two lines. On the first line, output Yes, and on the second line, output a,b,c in this order separated by spaces. a,b,c must satisfy −1018≤a,b,c≤1018 and (a,b,c)=(0,0,0).
If there are multiple solutions, any of them will be considered correct.
讯飞听见 翻译
###输出
如果不存在满足条件的行,则输出'no'。
如果存在满足条件的行,则输出两行。在第一行输出“yes”,在第二行按此顺序输出 a,b,c ,中间用空格分隔。 a,b,c 必须满足 −1018≤a,b,c≤1018 和 (a,b,c)=(0,0,0) 。
如果有多个解决方案,其中任何一个都将被认为是正确的。
Sample Input 1
Copy
3 1 1 3 2 2 4
Sample Output 1
Copy
Yes 2 1 -8
The line 2x+y−8=0 passes through the 2nd and 3rd points, so it satisfies the condition.
Sample Input 2
Copy
5 5 2 1 3 2 6 4 4 5 4
Sample Output 2
Copy
No
No line satisfying the condition exists.
Sample Input 3
Copy
11 -9374372 85232388 -60705467 86198234 -7475320 80628487 98066347 -23868213 -12177678 85284287 30535572 -35358356 51324557 22410787 28854279 44658587 -28804873 82911971 65052073 8819187 -67744430 68365758
Sample Output 3
Copy
Yes 4655800 4702358 -344340416016346
思路
随机数几组判断。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long n,x[500005],y[500005],q=0,p=0,lk=0;
int main(){
srand(time(0));
cin>>n;
for(int i=1;i<=n;i++){
cin>>x[i]>>y[i];
}
for(int i=1;i<=50;i++){
q=0;
p=0;
while(q==p){
q=rand()%n+1;
p=rand()%n+1;
}
if(x[q]==x[p]){
lk=0;
for(int j=1;j<=n;j++){
if(x[j]==x[p]){
lk++;
}
}
}
else if(y[q]==y[p]){
lk=0;
for(int j=1;j<=n;j++){
if(y[j]==y[p]){
lk++;
}
}
}
else{
lk=0;
for(int j=1;j<=n;j++){
if((y[j]-y[p])*(x[q]-x[p])==(y[q]-y[p])*(x[j]-x[p])){
lk++;
}
}
}
if(lk>=(n+1)/2){
cout<<"Yes"<<endl;
cout<<(y[q]-y[p])<<" "<<-(x[q]-x[p])<<" "<<-(y[q]-y[p])*x[p]+y[p]*(x[q]-x[p])<<endl;
return 0;
}
}
cout<<"No"<<endl;
return 0;
}

浙公网安备 33010602011771号