Fruit Ninja (随机算法)

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个点,定义一个完美切割,当M/N大于等于X(M是切一条直线后最多切到的点数),去找有没有一个完美切割。

分析:

1、直接去随机两个不同的点。

2、暴力判断有几个点在ab直线上,就是求M。

3、比较M*10和N*X*10的大小。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
struct node
{
    int x,y;
}p[MAX];
int n;
double x;
int k(int x1,int y1,int x2,int y2)
{
    return y1*x1-y2*x2;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int flag=0;
        scanf("%d%lf",&n,&x);
        for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
        int t=123;
        while(t--)
        {
            int a=rand()%n+1;
            int b=rand()%n+1;
            int sum=0;
            if(a==b)
                continue;

            for(int i=1;i<=n;i++)
            {
                if(k(p[a].x-p[b].x,p[i].y-p[a].y,p[i].x-p[a].x,p[a].y-p[b].y)==0)
                    sum++;
            }
            if(sum*10>=n*10*x)
            {
                flag=1;
                break;
            }
        }

        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ToBeYours/article/details/81448568
今日推荐