UVALive - 6955 Finding Lines 随机算法

题目链接:

http://acm.hust.edu.cn/vjudge/contest/126968#problem/F

题意

给你n个点,问是否有>=p/100*n个点共线(p>=20&&p<=100)。

题解

随机枚举两个点,判过这两条直线的点是否满足要求。

代码

#include<map>
#include<cmath>
#include<queue>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define pb(v) push_back(v)
#define sz() size()
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)

typedef long long  LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;

//start----------------------------------------------------------------------

const int maxn=1e5+10;
int n;
LL p;
pair<LL,LL> pt[maxn];

LL Cross(pair<LL,LL> p1,pair<LL,LL> p2){
	return p1.X*p2.Y-p1.Y*p2.X;
}

int main() {
	srand((unsigned)time(NULL));
	while(scanf("%d",&n)==1){
		scanf("%lld",&p); 
		rep(i,0,n) scanf("%lld%lld",&pt[i].X,&pt[i].Y);
		int cnt=1000;
		bool su=0;
		if(n==1){
			puts("possible"); continue;
		}
		while(cnt--){
			int a=rand()%n,b;
			do{ b=rand()%n; }while(b==a);
			int sum=0;
			rep(i,0,n){
				if(i!=a&&i!=b){
					if(!Cross(mkp(pt[i].X-pt[a].X,pt[i].Y-pt[a].Y),mkp(pt[i].X-pt[b].X,pt[i].Y-pt[b].Y))){
						sum++;
					}
				}
				if((sum+2)*100>=p*n){
					su=1; break;
				}
			}
			if(su) break;
		}
		if(su) puts("possible");
		else puts("impossible");
	} 
	return 0;
}

//end-----------------------------------------------------------------------
posted @ 2016-08-08 23:48  fenicnn  阅读(212)  评论(0编辑  收藏  举报