2013
12-02

Pick-up sticks

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

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.

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

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<list>
using namespace std;
#define precision 1e-8
#define MAX 1000000
struct point
{
double x;
double y;
};

struct seg
{
point a;
point b;
int cnt;
};

//p1a与p2a的叉积
double cross(point p1,point p2,point a)
{
return (p1.x-a.x)*(p2.y-a.y)-(p1.y-a.y)*(p2.x-a.x);
}
//判断m是否等于0
int dblcmp(double m)
{
if(fabs(m)<precision)
return 0;
else if(m<0)
return -1;
return 1;
}

int betweencmp(point p1,point p2,point a)
{

double s=(p1.x-a.x)*(p2.x-a.x)+(p1.y-a.y)*(p2.y-a.y);  //ap1与ap2的点积
return dblcmp(s);
}

int segcross(point a,point b,point c,point d)
{
double s1,s2,s3,s4;
int d1,d2,d3,d4;
d1=dblcmp(s1=cross(b,c,a));
d2=dblcmp(s2=cross(b,d,a));
d3=dblcmp(s3=cross(d,a,c));
d4=dblcmp(s4=cross(d,b,c));
//判断规范相交，并计算出交点
if(d1*d2<0 &&d3*d4<0)
{
return 1;
}
//判断非规范相交
if( ( d1==0 && betweencmp(a,b,c)<=0 ) ||
( d2==0 && betweencmp(a,b,d)<=0 ) ||
( d3==0 && betweencmp(c,d,a)<=0 ) ||
( d4==0 && betweencmp(c,d,b)<=0 ))
return true;

return false;
}

int main()
{
freopen("test.txt","r",stdin);
list<seg>stick;
int n;
int i,k;
int len;
list<seg>::iterator p;
seg v;

while(scanf("%d",&n)!=EOF &&n!=0)
{

scanf("%lf %lf %lf %lf", &v.a.x , &v.a.y , &v.b.x , &v.b.y);
v.cnt=1;
stick.push_back(v);
for(i=2;i<=n;i++)
{
scanf("%lf %lf %lf %lf", &v.a.x , &v.a.y , &v.b.x , &v.b.y);
v.cnt=i;
stick.push_back(v);
len=stick.size();
p=stick.begin();
for(k=1;k<len;k++)
{

if( segcross((*p).a,(*p).b,v.a,v.b) )
{
p=stick.erase(p);
}
else
p++;
}

}
printf("Top sticks: ");
p=stick.begin();
len=stick.size();
for(i=1;i<len;i++)
{
printf("%d, ",(*p).cnt);
p++;
}
printf("%d.\n",(*p).cnt);
stick.clear();
}
return 0;
}

1. 你的理解应该是：即使主持人拿走一个箱子对结果没有影响。这样想，主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率，但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

2. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮