牛客 Fruit Ninja 随机大法

链接:https://ac.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 MN\frac{M}{N}NM​ ≥ 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 >= x

题解:随机500次,找出两个点,然后判断这条线上的点数。神奇不。因为如果某条直线上有很多点,那么随机到这条线的概率就会很大。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4 + 10;
ll x[N], y[N];
int n;
double p;
int main() {
	int T;
	int l, r;
	int cnt;
	int flag;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %lf", &n, &p);
		for(int i = 1; i <= n; i++) scanf("%lld %lld", &x[i], &y[i]);
		flag = 0;
		for(int i = 1; i <= 500; i++) {
			l = rand() % n + 1;
			r = rand() % n + 1;
			if(l == r) continue;
			cnt = 0;
			for(int j = 1; j <= n; j++) {
				if((x[j] - x[l]) * (y[r] - y[j]) == (x[r] - x[j]) * (y[j] - y[l])) cnt++;
			}
			if(1.0 * cnt / n >= p) {
				flag = 1;
				break;
			}
		}
		printf(flag ? "Yes\n" : "No\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值