题目链接:poj 2653
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
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.
题意大概是按先后顺序往下扔一些线段,问在最上面的有多少条。即没有与它之后扔下的线条相交。
计算几何线段相交模板 + 队列时间复杂度优化。
虽然看到了hint但是不知为何仍然义无反顾的交了暴力,然后T。
队列的优化效果还是很明显的。
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define M 10000010
#define MM 100005
#define INF -0x7f7f7f7f
#define PI acos(-1)
using namespace std;
struct point//点相关计算,有的在这题中是多余的。
{
double x, y;
point(){}
point(double _x, double _y)
{
x = _x, y = _y;
}
point operator + (const point t)
{
return point(x + t.x, y + t.y);
}
point operator - (const point t)
{
return point(x - t.x, y - t.y);
}
point operator * (const double &a)//数乘
{
return point(x * a, y * a);
}
double operator * (const point t)//叉乘
{
return x * t.y - t.x * y;
}
double len(const point t)//两点间距
{
return sqrt((x - t.x) * (x - t.x) + (y - t.y) * (y - t.y));
}
};
struct line
{
point a, b;
int id;//线段编号
};
queue<line> Q;
bool check(line A, line B)//判断相交
{
if(min(A.a.x, A.b.x) <= max(B.a.x, B.b.x) && min(B.a.x, B.b.x) <= max(A.a.x, A.b.x) && min(A.a.y, A.b.y) <= max(B.a.y, B.b.y) && min(B.a.y, B.b.y) <= max(A.a.y, A.b.y))//快速排斥
{
if((((B.a - A.a) * (A.b - A.a)) * ((A.b - A.a) * (B.b - A.a)) >= 0) && (((A.a - B.a) * (B.b - B.a)) * ((B.b - B.a) * (A.b - B.a)) >= 0))//两次跨立判断
return true;
else
return false;
}
else
return false;
}
int main()
{
int T;
line l,t;
while(scanf("%d", &T) && T)
{
scanf("%lf %lf %lf %lf", &l.a.x, &l.a.y, &l.b.x, &l.b.y);
l.id = 1;
Q.push(l);
for(int i = 2; i <= T; i++)
{
scanf("%lf %lf %lf %lf", &t.a.x, &t.a.y, &t.b.x, &t.b.y);
t.id = i;
Q.push(t);
while(!Q.empty())
{
l = Q.front(); Q.pop();
if(t.id == l.id)//循环判断一次后遇到自己,重新入队后跳出循环。
{
Q.push(t);
break;
}
if(!check(l, t)) Q.push(l);
}
}
printf("Top sticks: %d", Q.front().id);
Q.pop();
while(!Q.empty())
{
printf(", %d", Q.front().id);
Q.pop();
}
printf(".\n");
}
return 0;
}
运行结果: