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-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5751445.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值