给出n个点,求是否存在一条直线,使得落在直线上的点占所有点的p%以上。
n<=100000
2<=p<=100
#include<iostream> #include<cstdio> #include<cstring> #include<ctime> #include<cstdlib> #define maxn 100010 using namespace std; int n; int p; struct node{ long long x,y; }a[maxn]; bool check(int id1,int id2){ node line; line.x=a[id1].x-a[id2].x; line.y=a[id1].y-a[id2].y; int num=2; for(int i=0;i<n;i++){ if(i==id1||i==id2)continue; node line2; line2.x=a[i].x-a[id1].x; line2.y=a[i].y-a[id1].y; if(line.x*line2.y==line.y*line2.x) num++; } if(100*num>=p*n)return 1; return 0; } int main(){ srand(time(0));rand();rand(); scanf("%d%d",&n,&p); if(n==1){ puts("possible"); return 0; } for(int i=0;i<n;i++) scanf("%lld%lld",&a[i].x,&a[i].y); for(int i=1;i<=300;i++){ int id1=rand()*rand()%n; int id2=rand()*rand()%n; if(id1==id2){ if(id1==n-1)id2--; else id2++; } if(check(id1,id2)){ puts("possible"); return 0; } } puts("impossible"); return 0; }
来源:https://www.cnblogs.com/thmyl/p/12297373.html