2018————上海acm——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

题意:图上有m个点问你一条直线能不能穿过m*x个点其中x是0-1之间的;

思路:随机在图上取两个点,在从图上取个点如果能连成直线就count+1最后判断是不是大于等于m*x

#include <bits/stdc++.h>

using namespace std;
int d[10005],d2[10005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {int i,j;
        double x;
        int n,a,b;
        scanf("%d%lf",&n,&x);
       for(i=0;i<n;i++)
        scanf("%d %d",&d[i],&d2[i]);   
       int cnt=0,flag=0;
        for(i=0;i<200&&!flag;i++)
        {
           a=rand()%n,b=rand()%n;    //随机从图上选点
//cout<<a<<b<<endl;
            if(a==b)        //如果两个点选的一样就从新选
                continue;
            else
            {cnt=0;   //这里让cnt为0是因为下面选点是全部都选的,你随机的a,b都会被选并且成立,所以从0开始而不是从2开始  
                for(j=0;j<n;j++)
                {
                    if((d2[j]-d2[a])*(d[j]-d[b])==(d2[j]-d2[b])*(d[j]-d[a]))  //两直线平行的判断方法
                        {cnt++;
                        //cout<<d2[a]<<" "<<d2[j]<<endl;
                        }
                }
                if(cnt>=n*x)
                {flag=1;
                    break;
                }
           }
        }
        printf(flag ==1 ?"Yes\n":"No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wearegamer/article/details/81449737
今日推荐