uvalive 6955 Finding Lines rand() 随机算法

【题目链接】点击打开链接

【题意】给了两个数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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值