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個點裏枚舉兩個點,看兩個點是否能和其他的點斜率相同,當相同的個數超過比例數時就符合條件;

           隨機的次數,我隨機了1000個點,這樣過的概率就很大了,應該不會wa;

#include<bits/stdc++.h>
using namespace std;
struct Point
{
    int x,y;
}p[10005];
int judge(int x,int y,int a,int b)
{
    return a*y-b*x;
}
int main()
{
    int n,a,b,t,ans;
    double mod;
    cin>>t;
    while(t--)
    {
        cin>>n>>mod;
        for(int i=0;i<n;i++)
        {
            cin>>p[i].x>>p[i].y;
        }
        int sum=1001;
        while(sum--)
        {
            ans=0;
            a=rand()%n;//隨機生成從0~n-1,之間的數
            b=rand()%n;
            if(a==b)
            {
                continue;
            }
            for(int i=0;i<n;i++)
            {
                if(judge(p[i].x-p[a].x,p[i].y-p[a].y,p[b].x-p[a].x,p[b].y-p[a].y)==0)//斜率比較
                {
                    ans++;
                }
            }
            if(ans*100>=100*n*mod)//看總數是否符合
            {
                cout<<"Yes"<<endl;
                break;
            }
        }
        if(sum==-1)//當隨機了1000次後還沒有符合的就輸出No
        {
            cout<<"No"<<endl;
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章