Pick-up sticks
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 9009 | Accepted: 3323 |
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.
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.
Hint
Huge input,scanf is recommended.
//这道题WA了那么多次的原因是,首先是爆搜要讲究方法,开始时从头开始到后,中间虽然知道了改进的方法,但是
//却改不过来,只是知道了应该从后到前,后来才知道了是模拟出现了错误,没有把最致命的b[j]改成b[i].....
//这道题WA了那么多次的原因是,首先是爆搜要讲究方法,开始时从头开始到后,中间虽然知道了改进的方法,但是
//却改不过来,只是知道了应该从后到前,后来才知道了是模拟出现了错误,没有把最致命的b[j]改成b[i].....
#include <stdio.h>
struct point
{
double x1;
double x2;
double y1;
double y2;
}a[100005];
int b[100005];
int main (void)
{
//freopen("2653.txt","r",stdin);
int n,i,j;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
b[i]=1;
}
for(i=n-1;i>0;i--)
{
for(j=i+1;j<=n;j++)
{
if((((a[j].x1-a[i].x1)*(a[i].y2-a[i].y1)-(a[j].y1-a[i].y1)*(a[i].x2-a[i].x1))*((a[j].x2-a[i].x1)*(a[i].y2-a[i].y1)-(a[j].y2-a[i].y1)*(a[i].x2-a[i].x1))<0)&&((((a[i].x1-a[j].x2)*(a[j].y1-a[j].y2)-(a[i].y1-a[j].y2)*(a[j].x1-a[j].x2))*((a[i].x2-a[j].x2)*(a[j].y1-a[j].y2)-(a[i].y2-a[j].y2)*(a[j].x1-a[j].x2)))<0))
{
b[i]=0;
break;
}
}
}
printf("Top sticks:");
for(j=1;j<=n;j++)
{
if(b[j])
{
if(j<n)
printf(" %d,",j);
else
printf(" %d.",j);
}
}
printf("\n");
}
return 0;
}