链接:https://ac.nowcoder.com/acm/contest/163/A?&headNav=www 来源:牛客网
题目描述
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 MNMN ≥ 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
题意:
给出n个点的坐标,问是否存在一条直线使直线上点的个数m,使 m/n>x
思路:
先用随机数产生两个点形成线,然后从已知的点集合中枚举,判断是否共线(利用斜率相同),找到满足条件M/N >= x 的M 即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstring>
#include <stack>
#include <set>
#include <vector>
#include <map>
#include <time.h>
#include <queue>
#define Swap(a,b) a ^= b ^= a ^= b
#define pi acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define lson rt<<1
#define rson rt<<1|1
using namespace std ;
typedef long long LL;
//const int N = 1e7+10 ;
const int inf = 0x3f3f3f3f;
const int MAX = 1e6+5;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node {
int x ;
int y ;
};
node a[MAX] ;
bool check(const node & a , const node &b ,const node &c){
return (c.x-a.x)*(b.y-a.y)==(b.x-a.x)*(c.y-a.y);
}
void init()
{
srand((unsigned)time(NULL));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T ;
init() ;
cin >>T;
while(T-- ){
int n ;
double x ;
bool flag = false ;
cin >> n >>x ;
for(int i = 1 ;i<=n ;i++)cin >> a[i].x >> a[i].y ;
for(int i = 0 ; i<250 ; i++){
int p1 = rand()%(n+1) ;
int p2 = rand()%(n+1) ;
if(p1 == p2 )continue ;
if(p1 <1 || p1>n || p2 <1 || p2>n ) continue ;
int m = 2 ;
for(int j = 1 ; j<=n ; j++ ){
if(j == p1 || j==p2 ) continue ;
if(check(a[p1] ,a[p2],a[j])) m++ ;
}
if(m*1.0/n >=x ){
flag = true ;
break ;
}
}
if(flag) cout<<"Yes"<<endl ;
else cout<<"No"<<endl ;
}
return 0 ;
}