hdu1147

来源:互联网 发布:grpc golang 编辑:程序博客网 时间:2024/06/11 01:28

Pick-up sticks

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
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
51 1 4 22 3 3 11 -2.0 8 41 4 8 23 3 6 -2.030 0 1 11 0 2 12 0 3 10
 

Sample Output
Top sticks: 2, 4, 5.Top sticks: 1, 2, 3.
很明显的计算几何题目但是在操作的过程中如果用叉积处理相交问题的话需要注意的是,在叉积小于0时还存在两条向量平行但是不共线的情况,这时就需要加条件来排除这种情况,,在遍历查找时,需要注意的时从前向后查找是否被覆盖,如果从后向前遍历的话,会由于不必要的额外操作次数而出现超时的情况,在从前向后遍历时,如果出现被覆盖的情况,就直接跳出循环,代码如下
#include <iostream>#include <cstdio>#include<cstring>using namespace std;const int maxn=100100;struct node{    double x1,y1;    double x2,y2;};struct dot{    int x,y;};node a[maxn];int cross(node a,node b){   if((((b.x2-b.x1)*(a.y1-b.y1)-(b.y2-b.y1)*(a.x1-b.x1))*((b.x2-b.x1)*(a.y2-b.y1)-(b.y2-b.y1)*(a.x2-b.x1))<=0)&&(((a.x2-a.x1)*(b.y1-a.y1)-(a.y2-a.y1)*(b.x1-a.x1))*((a.x2-a.x1)*(b.y2-a.y1)-(a.y2-a.y1)*(b.x2-a.x1)))<=0)        {            if((b.x2-b.x1)*(a.y2-a.y1)-(b.y2-b.y1)*(a.x2-a.x1)==0&&(b.x1!=a.x1||b.y1!=a.y1)&&(b.x1!=a.x2||b.y1!=a.y2))//a与b平行且a b不公线                return 0;            else             return 1;        }        return 0;}int main(){    int vis[100100];    int n;    while(scanf("%d",&n)!=EOF&&n)    {        memset(vis,1,sizeof(vis));         for(int i=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);        }        for(int i=0;i<n-1;i++)        {            for(int j=i+1;j<n;j++)            {                if(cross(a[i],a[j]))                {                    vis[i]=0;                    break;                }            }        }        printf("Top sticks: ");        for(int i=0;i<n-1;i++)            if(vis[i])                printf("%d, ",i+1);            printf("%d.\n",n);    }    return 0;}


0 0
原创粉丝点击