UVA6955 Finding Lines (random)

传送门:https://vjudge.net/contest/146124#problem/A

题意:给N个点,问是否存在一条直线经过$p\%$个点。

思路:暴力枚举妥妥的TLE,随机大法好!因为 $p\geq20$,所以迭代200次,

如果存在一条合法直线,但我萌没发现它的概率大于: $1 - 0.8^{200}$如果

这都没找到,那买彩票去吧!

PS: $n\leq2$时需要特判一下。

#include <iostream>
#include <cstdio>
using namespace std;
const int NICO = 100000+10;
typedef long long LL;
int n, p;
LL x[NICO], y[NICO];
bool check(int a, int b)
{
    int cnt = 0;
    for(int i=0;i<n;i++)
    {
        if( (y[i]-y[b]) * (x[i]-x[a]) == (y[i]-y[a])*(x[i]-x[b]) ) cnt ++;
    }
    return cnt >= (n*p+99)/100;
}
int main()
{
    while(~scanf("%d %d", &n, &p))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lld %lld", &x[i], &y[i]);
        }
        int ok = 0;
        for(int i=1;i<=200;i++)
        {
            int a = rand()%n;
            int b = rand()%n;
            if(a == b) continue;//两点是同一个点的话,小心了。
            if(check(a, b)) ok = 1;
        }
        printf("%s\n", (ok||n<=2)?"possible":"impossible");
    }
}

  

posted @ 2017-05-10 16:50  RUSH_D_CAT  阅读(158)  评论(0编辑  收藏  举报