这题就是大模拟,用队列来保证这些线段的先后顺序,再用一个队列来辅助存储——就是类似于滚动数组那样,再开两个int类型的队列存编号,原理同上。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <utility>
using namespace std;
struct P
{
double x, y;
P() {}
P(double x, double y) : x(x), y(y) {}
P operator +(const P &p)
{
return P(x + p.x, y + p.y);
}
P operator -(const P &p)
{
return P(x - p.x, y - p.y);
}
P operator *(const double &d)
{
return P(x * d, y * d);
}
P operator /(const double &d)
{
return P(x / d, y / d);
}
bool operator ==(const P &p)
{
return x == p.x && y == p.y;
}
P operator =(const P &p)
{
x = p.x, y = p.y;
return *this;
}
double det(const P &p)
{
return x * p.y - y * p.x;
}
double dot(const P &p)
{
return x * p.x + y * p.y;
}
void show()
{
cout << x << " " << y << " ";
}
};
queue< pair<P, P> > seg_que0, seg_que1;
queue<int> no_que0, no_que1;
inline void init()
{
while(seg_que0.size()) seg_que0.pop();
while(seg_que1.size()) seg_que1.pop();
while(no_que0.size()) no_que0.pop();
while(no_que1.size()) no_que1.pop();
}
inline bool on_point(P p1, P p2, P q1, P q2)
{
return p1 == q2 || p1 == q1 || p2 == q1 || p2 == q2;
}
inline bool on_seg(P p1, P p2, P q)
{
return (q - p1).det(p2 - q) == 0 && (q - p1).dot(p2 - q) >= 0;
}
inline bool on_inster(P p1, P p2, P q1, P q2)
{
return ((p1 - q2).det(q1 - q2)) * ((q1 - q2).det(p2 - q2)) > 0 && ((q1 - p2).det(p1 - p2)) * ((p1 - p2).det(q2 - p2)) > 0;
}
int main()
{
int n, i, j, k;
while(scanf("%d", &n) != EOF && n != 0)
{
init();
P start, fine;
for(i = 0; i < n; ++i)
{
if(i == 0)
{
scanf("%lf %lf %lf %lf", &start.x, &start.y, &fine.x, &fine.y);
seg_que0.push(pair<P, P>(start, fine));
no_que0.push(i);
}
else
{
scanf("%lf %lf %lf %lf", &start.x, &start.y, &fine.x, &fine.y);
if(i % 2 == 0)
{
while(seg_que1.size())
{
pair<P, P> tmp = seg_que1.front();
seg_que1.pop();
int pos = no_que1.front();
no_que1.pop();
if(on_point(start, fine, tmp.first, tmp.second)) continue;
else if(on_seg(start, fine, tmp.first) || on_seg(start, fine, tmp.second) ||
on_seg(tmp.first, tmp.second, start) || on_seg(tmp.first, tmp.second, fine)) continue;
else if(on_inster(start, fine, tmp.first, tmp.second)) continue;
else
{
seg_que0.push(tmp);
no_que0.push(pos);
}
}
seg_que0.push(pair<P, P>(start, fine));
no_que0.push(i);
}
else
{
while(seg_que0.size())
{
pair<P, P> tmp = seg_que0.front();
seg_que0.pop();
int pos = no_que0.front();
no_que0.pop();
if(on_point(start, fine, tmp.first, tmp.second)) continue;
else if(on_seg(start, fine, tmp.first) || on_seg(start, fine, tmp.second) ||
on_seg(tmp.first, tmp.second, start) || on_seg(tmp.first, tmp.second, fine)) continue;
else if(on_inster(start, fine, tmp.first, tmp.second)) continue;
else
{
seg_que1.push(tmp);
no_que1.push(pos);
}
}
seg_que1.push(pair<P, P>(start, fine));
no_que1.push(i);
}
}
}
printf("Top sticks: ");
if(n % 2 == 0)
{
k = 0;
while(no_que1.size() != 0)
{
if(k != 0) printf(", ");
printf("%d", no_que1.front() + 1);
no_que1.pop();
k += 1;
}
}
else
{
k = 0;
while(no_que0.size() != 0)
{
if(k != 0) printf(", ");
printf("%d", no_que0.front() + 1);
no_que0.pop();
k += 1;
}
}
printf(".");
puts("");
}
return 0;
}