Pick-up sticks

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3297    Accepted Submission(s): 1228

Problem Description
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
 
Input
Input consists of a number of cases. The data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
 
Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
 
Sample Input
5 1 1 4 2 2 3 3 1 1 -2.0 8 4 1 4 8 2 3 3 6 -2.0 3 0 0 1 1 1 0 2 1 2 0 3 1 0
 
Sample Output
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.
题目大意
有多组数据,每一组有n个线段,现在将这些线段按照输入的顺序放到坐标系里面,如果新加入的线段和已加入的线段有重合,就把所有和它相交的线段去掉然后把它放上去。询问最后还有在坐标系里的线段。
题解:
一道十分暴力的题目,数据比较水,O(n^2)可以过,因为没有100000条线段全部平行的情况。
一个一个从前往后找是否有和它相交的线段就好,注意一下最后要输出一个“.”
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
struct Vector
{
    double x,y;
    Vector(){}
    Vector(double xx,double yy){x=xx;y=yy;}
    Vector operator+(Vector b)
    {
        Vector ans;
        ans.x=x+b.x;ans.y=y+b.y;
        return ans;
    }
    Vector operator-(Vector b)
    {
        Vector ans;
        ans.x=x-b.x;ans.y=y-b.y;
        return ans;
    }
    double operator*(Vector b)
    {
        return x*b.y-b.x*y;
    }
    double operator^(Vector b)
    {
        return x*b.x+y*b.y;
    }
};
struct node
{
    Vector a,b;
}segment[100001];
bool judge(node a,node b)
{
    if(min(a.a.x,a.b.x)<=max(b.a.x,b.b.x)&&max(a.a.x,a.b.x)>=min(b.a.x,b.b.x)&&min(a.a.y,a.b.y)<=max(b.a.y,b.b.y)&&max(a.a.y,a.b.y)>=min(b.a.y,b.b.y))
    {
        if(((a.a-a.b)*(b.a-a.b))*((a.a-a.b)*(b.b-a.b))<=0&&((b.a-b.b)*(a.a-b.b))*((b.a-b.b)*(a.b-b.b))<=0)return true;
    }
    return false;
}
int main()
{
    int i,j;
    while(1)
    {
        scanf("%d",&n);if(n==0)break;
        for(i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&segment[i].a.x,&segment[i].a.y,&segment[i].b.x,&segment[i].b.y);
        printf("Top sticks:");
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(judge(segment[i],segment[j]))break;    
            }
            if(j>n)printf(" %d,",i);
        }
        printf(" %d.\n",n);
    }
    return 0;
}

 

posted @ 2017-08-03 14:42  kakakakakaka  阅读(310)  评论(0编辑  收藏  举报

Never forget why you start

//鼠标爆炸特效