HDU 1147 Pick-up sticks(计算几何 判断直线相交)

来源:互联网 发布:java list 泛型写法 编辑:程序博客网 时间:2024/06/06 01:05

Pick-up sticks

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


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.
 

Source
University of Waterloo Local Contest 2005.09.17
 

Recommend
Eddy
 

这个题目显然是判断直线的规范相交问题,看一下O(N^2)的代码一下就写出来了,不过会超时
不过也不是不能搞定,还是原来的思想,做一点小优化就OK了,这个优化就是记录比当前编号小
的且没有被覆盖的点,这样就可以省略大量多余的判断,因为100000个点,平方的复杂度就算
cpu空转这么多次循环时间你也受不了!
#include <iostream>#include <string.h>#include <math.h>#include <stdio.h>#include <algorithm>using namespace std;#define eps 1e-8struct point{double x;double y;};struct line{    point a;    point b;}po[110000];bool rec[110000];int map[110000];double multi(point p0, point p1, point p2)//j计算差乘{   return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}bool is_cross(point &s1,point &e1,point &s2,point &e2)//判断线段是否相交(非规范相交){   return max(s1.x,e1.x) > min(s2.x,e2.x)&&          max(s2.x,e2.x) > min(s1.x,e1.x)&&          max(s1.y,e1.y) > min(s2.y,e2.y)&&          max(s2.y,e2.y) > min(s1.y,e1.y)&&          multi(s1,e1,s2)*multi(s1,e1,e2) < 0&&          multi(s2,e2,s1)*multi(s2,e2,e1) < 0;}int main(){    int n;    int i,k,j,r;    while(scanf("%d",&n),n)    {        memset(rec,1,sizeof(rec));        for(i=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&po[i].a.x,&po[i].a.y,&po[i].b.x,&po[i].b.y);            map[i]=i;        }        k=n-1;        for(i=n-1;i>=1;i--)        {        for(j=0,r=0;j<k;j++)        {            if(rec[map[j]] && map[j] < i)            if(is_cross(po[i].a,po[i].b,po[map[j]].a,po[map[j]].b))            rec[map[j]]=0;            else            map[r++]=map[j];        }        k=r;        }        printf("Top sticks:");        for(i=0;i<n;i++)        if(rec[i])           {                printf(" %d",i+1);                break;           }           i++;           for(i;i<n;i++)           if(rec[i])           printf(", %d",i+1);           printf(".\n");    }    return 0;}


原创粉丝点击