HDU 1147 Pick-up sticks(計算幾何 判斷直線相交)

來源:互聯網
上載者:User
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 DescriptionStan 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.
 


InputInput 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.  


OutputFor 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.
 


SourceUniversity of Waterloo Local Contest 2005.09.17 


RecommendEddy 這個題目顯然是判斷直線的規範相交問題,看一下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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.