原题:https://www.nowcoder.com/acm/contest/163/A
题目描述
Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy,
splat and satisfying fruit carnage! Become the ultimate bringer of sweet, tasty destruction with every slash.
Fruit Ninja is a very popular game on cell phones where people can enjoy cutting the fruit by touching the screen.
In this problem, the screen is rectangular, and all the fruits can be considered as a point. A touch is a straight line cutting
thought the whole screen, all the fruits in the line will be cut.
A touch is EXCELLENT if ≥ x, (N is total number of fruits in the screen, M is the number of fruits that cut by the touch, x is a real number.)
Now you are given N fruits position in the screen, you want to know if exist a EXCELLENT touch.
输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve. The first line of each case contains an integer N (1 ≤ N ≤ 104) and a real number x (0 < x < 1), as mentioned above. The real number will have only 1 digit after the decimal point. The next N lines, each lines contains two integers xi and yi (-109 ≤ xi,yi ≤ 109), denotes the coordinates of a fruit.
输出描述:
For each test case, output "Yes" if there are at least one EXCELLENT touch. Otherwise, output "No".
示例1
输入
2 5 0.6 -1 -1 20 1 1 20 5 5 9 9 5 0.5 -1 -1 20 1 1 20 2 5 9 9
输出
Yes No
分析:做的时候没有想到使用随机数的方法来做,第一次遇到使用使用这种方法的题目(菜鸡做题太少),一开始一直超时不知道怎么解决,如果使用随机数的做法就能够把复杂度限制在le6的范围,这样就不用担心超时了。
ac代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000+100;
int t, n;
double x;
struct node{
int x, y;
node(){}
}p[maxn];
bool fun(node a, node b, node c){
return (c.x-a.x)*(b.y-a.y) == (b.x-a.x)*(c.y-a.y);
}
int main(){
scanf("%d", &t);
while(t--){
scanf("%d%lf", &n, &x);
for(int i=1; i<=n; i++){
scanf("%d%d", &p[i].x, &p[i].y);
}
int flag = 0;
int res = ceil(n*x);
for(int i=1; i<=250; i++){
int p1 = rand()%(n+1);
int p2 = rand()%(n+2);
if(p1 == p2) continue;
if(p1<1 || p1>n || p2<1 || p2>n) continue;
int cnt = 2;
for(int j=1; j<=n; j++){
if(j==p1 || j==p2) continue;
if(fun(p[p1], p[p2], p[j])) cnt++;
}
if(cnt >= res){
flag = 1;
break;
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}