【题目链接】点击打开链接
【题意】给了两个数n,p。问在平面上是否存在一条直线经过ceil(n*p/100)个点。
【解题方法】随机算法。由于O(n*n)显然会超时,我们这里应用随机算法来找直线。为什么可以用随机算法呢?这里p<=20,所以选取两个点在一条直线上的概率为1/25。也就是选取两个点不在一条直线上的概率为24/25,那么经过多次随机函数之后,这个值为(24/25)^k,k取大之后,这个值趋近于e-k次方,即是(24/25)^25*k~e^(-k)。所以就可以大胆随机,不用怕WA了。 更细讲解吗可以看这里:点击打开链接
//
//Created by just_sort 2016/1/2
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define MP(x, y) make_pair(x,y)
const int maxn = 100010;
const int maxm = 2e5;
const int maxs = 10;
const int INF = 1e9;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
int n; double p;
int x[maxn], y[maxn];
int main()
{
int t;
while(scanf("%d", &n) != EOF){
scanf("%lf", &p);
t = n * p;
srand(time(0));
REP1(i, 0, n) scanf("%d%d", &x[i], &y[i]);
bool ok = 0;
if(n <= 2){
printf("possible\n");
continue;
}
int cnt;
REP1(i, 0, 400){
cnt = 2;
int u = rand() % n, v;
while(1){
v = rand() % n;
if(u != v) break;
}
REP1(k, 0, n){
if(k == u || k == v) continue;
int tt = (x[k] - x[u]) * (y[v] - y[u]) - (y[k] - y[u]) * (x[v] - x[u]);
if(tt == 0) cnt++;
if(cnt * 100 >= t) ok = 1;
if(ok) break;
}
if(ok) break;
}
if(ok) printf("possible\n");
else printf("impossible\n");
}
return 0;
}



京公网安备 11010502036488号