求平面上在一条直线上的点的个数

这样的题差不多有点类似,都是先按平面上的斜率排序,O(n*n)

Lucky Boy

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 1   Accepted: 1  

Description

Recently, Lur have a good luck. He is also the cleverest boy in his school as he create the most popular computer game – Lucky Boy. The game is played by two players, a and b, in 2d planar .In the game Lucky Boy, there are n different points on plane, each time one can remove one or multiple co-line points from the plane. The one who can firstly remove more than two points from the plane wins. The one who removes the last point on the plane can also win the game. You may assume that two players are both clever enough that they can always make the best choice. The winner is called Lucky Boy.
Given the n points, can you tell me who will be the Lucky Boy ? Note that player a will always the first one to remove points from the plane.

Input

The first line of each case is an integer n(0<n<= 10^3), following n lines each contains two integers x and y( 0<=x, y<=10^8), describing the coordinates of each point. Ended by EOF.

Output

Output “a is the lucky boy.” in a single line if a win the game, otherwise you should output  “b is the lucky boy.” in a single line.

Sample Input

3
0 0
1 1
2 2
3
0 0
1 1
2 3

Sample Output

a is the lucky boy.
b is the lucky boy.

Source

2011 Heilongjiang collegiate programming contest

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>

using namespace std;

int a[1010],b[1010];
struct node
{
    double k,b;    
}s[500000];
int cmp(node x,node y)
{
    if(x.k!=y.k)return x.k<y.k;
    return  x.b<y.b;     
}
int main()
{
    int i,j,k,l;
    int n;
   while(scanf("%d",&n)==1)
   {
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i],&b[i]);        
        }  
        double k1,m;    
        int y1,y2,x1,x2; 
        int ans=0;
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                x1=a[i];y1=b[i];
                x2=a[j];y2=b[j];
                if(x2==x1) 
                {
                    k1=1<<30;
                    m=x1;    
                }              
               else 
               { 
                    k1=(y2-y1)*1.0/(x2-x1);
                    m=y1-k1*x1;
                }
                s[ans].k=k1;
                s[ans++].b=m;
            }    
        }
        sort(s,s+ans,cmp);
        int mmax=1;
        for(i=1;i<ans;i++)
        {
            int temp=1;
            while(i<ans&&fabs(s[i].b-s[i-1].b)<1e-9&&fabs(s[i].k-s[i-1].k)<1e-9)
            {
                i++;
                temp++;                
            } 
            mmax=max(mmax,temp);
            if(mmax>=3) break;
        }
        if(mmax>=3) puts("a is the lucky boy.");
        else
        {
             if(n%3!=0)    puts("a is the lucky boy.");
             else puts("b is the lucky boy.");
        }
    }
    return 0;
}

http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=8252#problem/G

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define Maxinf 2147483647
using namespace std;
int a[1000],b[1000];
struct node
{
    double k,b;    
}s[500000];
int cmp(node x,node y)
{
    if(x.k!=y.k)return x.k<y.k;
    return  x.b<y.b;     
}
int main()
{
    int t;
    //double temp=1e-9;
    //printf("%lf",temp);
    scanf("%d",&t);
    int i,j,k,l;
    for(l=1;l<=t;l++)
    {
        int n;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i],&b[i]);        
        }  
        //printf("dfdfd");
        if(n<=2) {printf("Case %d: ",l);printf("%d\n",n);continue;}
        // printf("dfdfd");
        double k1,m;    
        int y1,y2,x1,x2; 
        int ans=0;
        
        //cout<<endl;
        
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                x1=a[i];y1=b[i];
                x2=a[j];y2=b[j];
               // cout<<x1-x2<<endl;
                if(x2==x1) 
                {
                    k1=x1;
                    m=1<<30;    
                }              
               else 
               { 
                    k1=(y2-y1)*1.0/(x2-x1);
                    m=y1-k1*x1;
                }
                s[ans].k=k1;
                s[ans++].b=m;
            }    
        }
        sort(s,s+ans,cmp);
        //for(i=0;i<ans;i++) printf("%lf %lf\n",s[i].k,s[i].b);
        int mmax=1;
        for(i=1;i<ans;i++)
        {
            int temp=1;
            while(i<ans&&fabs(s[i].b-s[i-1].b)<1e-9&&fabs(s[i].k-s[i-1].k)<1e-9)
            {
                i++;
                temp++;                
            } 
            mmax=max(mmax,temp);
        }
        for(i=2;i<=700;i++) if(i*(i-1)/2==mmax) break;
        //if(i==701) while(1);
        printf("Case %d: ",l);
        printf("%d\n",i);
    }
}


你可能感兴趣的:(求平面上在一条直线上的点的个数)