1. 程式人生 > >2018大都會賽 A Fruit Ninja【隨機數】

2018大都會賽 A Fruit Ninja【隨機數】

spl eps 分享圖片 fine pre bre tps c++ --

題目鏈接:戳這裏

題意:一個平面裏有n個點,問存不存在一條直線上有m個點,滿足m >= n*x。

解題思路:0<x<1,且x小數點後只有1位,也就是說10*m > n。假設存在一條直線滿足條件,則任意一點在m中的概率>0.1,也就是說理論上隨機10個點,一定有一個點在m上。所以隨機一個點,遍歷與其他點的斜率,斜率相同的點 + 該點本身 >= n * m,則符合條件。

附ac代碼:

技術分享圖片
 1 #include <iostream>
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long
long ll; 5 #define lson l,mid,rt<<1 6 #define rson mid+1,r,rt<<1|1 7 #define lef rt<<1 8 #define rig rt<<1|1 9 const int maxn = 2e5 + 10; 10 const ll mod = 998244353; 11 const ll inf = 0x3f3f3f3f; 12 const double eps = 1e-6; 13 struct nod 14 { 15 double x; 16 double
y; 17 }coo[maxn]; 18 19 int main() 20 { 21 int t; 22 scanf("%d", &t); 23 double x; 24 int n; 25 while(t--) 26 { 27 28 scanf("%d %lf", &n, &x); 29 for(int i = 1; i <= n; ++i) 30 { 31 scanf("%lf %lf", &coo[i].x, &coo[i].y);
32 } 33 int cas = 20; 34 while(cas--) 35 { 36 map<double, double> mp; 37 int i = rand() % n + 1, j = 1; 38 for(j = 1; j <= n; ++j) 39 { 40 if(i == j)continue; 41 double u; 42 if(fabs(coo[j].x - coo[i].x) < eps) 43 { 44 u = inf + 1.0; 45 ++mp[u]; 46 if(mp[u] + 1.0 + eps > n * x) break; 47 } 48 else 49 { 50 u = (coo[i].y - coo[j].y) / (coo[i].x - coo[j].x); 51 ++mp[u]; 52 if(mp[u] + 1.0 + eps > n * x) break; 53 } 54 } 55 if(j <= n) break; 56 } 57 if(cas > 0) puts("Yes"); 58 else puts("No"); 59 } 60 return 0; 61 }
View Code

2018大都會賽 A Fruit Ninja【隨機數】