Pick-up sticks
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 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.
Source |
[Submit] [Go Back] [Status] [Discuss]
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
typedef double PointType;
const int maxn=100000+100;
struct point{
PointType x,y;
point(double x=0,double y=0):x(x),y(y) {}
};
typedef point Vector ;
Vector operator + (Vector A , Vector B){
return Vector(A.x + B.x , A.y + B.y);
}
Vector operator - (point A , point B){
return Vector(A.x - B.x , A.y - B.y);
}
Vector operator * (Vector A , double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A , double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const point& a, const point& b){
return a.x<b.x||(a.x==b.x && a.y<b.y);
}
int dcmp(double x){
if(fabs(x) < eps) return 0;else return x<0 ?-1:1;
}
double Dot(Vector A , Vector B){
return A.x*B.x+A.y*B.y;
}
double Cross(Vector A , Vector B){
return A.x*B.y - A.y*B.x;
}
bool Onsegment(point p,point a1,point a2)
{
return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p)) <0;
}
bool Segmentproperintersection(point a1,point a2,point b1,point b2)
{
if(Onsegment(a1,b1,b2) || Onsegment(a2,b1,b2) || Onsegment(b1,a1,a2) || Onsegment(b2,a1,a2))
return true;
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
struct node{
point a,b;
}data[maxn];
int vis[maxn];
int main()
{
int n;
while(cin>>n && n)
{
CLR(vis);
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf%lf",&data[i].a.x,&data[i].a.y,&data[i].b.x,&data[i].b.y);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(Segmentproperintersection(data[i].a,data[i].b,data[j].a,data[j].b))
{
vis[i]=1;
break;
}
}
}
int flag=0;
printf("Top sticks:");
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
if(!flag)
printf(" %d",i),flag=1;
else
printf(", %d",i);
}
}
printf(".\n");
}
return 0;
}