2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 A Fruit Ninja (随机大法)

题目链接: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

题意:求一条直线,至少经过 N 个点中的 N * k 个点,(0.1≤k≤0.9)。k是一个大于0小于1的小数.所以可以考虑下随机数大法。

随机选择2个点构成一条线,然后判断这条线上有多少个点,当ans>=N*k 即可,随机大法好。

#include<bits/stdc++.h>
using namespace std;

struct node{
	int x;
	int y;
}no[10005];
bool check(int a,int b,int j)
{
	int temp=no[j].y-no[a].y;
	int Temp=no[j].x-no[a].x;
	
	int t=no[j].y-no[b].y;
	int T=no[j].x-no[b].x;
	if(temp*T-Temp*t==0)
		return 1;
	return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	double x;
	srand(time(0));
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n>>x;
		for(int i=0;i<n;i++)
			cin>>no[i].x>>no[i].y;
		int flag=0;
		for(int i=0;i<600&&flag==0;i++)
		{
			int a=rand()%n;
			int b=rand()%n;
			if(a==b)
				continue;
			int ans=2;
			for(int j=0;j<n;j++)
			{
				if(check(a,b,j))
					ans++;
				if(ans>=n*x)
				{
					flag=1;
					break;
				}
			}
		}
		if(flag)
            cout<<"Yes\n";
		else
            cout<<"No\n";
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/passer__/article/details/81452273
今日推荐