暴力题
注意vector用法,erase函数返回的是删除后的下一个元素的指针。迭代器的写法是:vector< >::iterator i;
View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define maxn 100005
#define eps 1e-10
struct Point
{
double x, y;
};
struct Line
{
Point a, b;
} line[maxn];
vector <int> stk(maxn);
int n;
void input()
{
for (int i = 0; i < n; i++)
scanf("%lf%lf%lf%lf", &line[i].a.x, &line[i].a.y, &line[i].b.x, &line[i].b.y);
}
bool inter(Point &a, Point &b, Point &c, Point &d)
{
if (min(a.x, b.x) > max(c.x, d.x) || min(a.y, b.y) > max(c.y, d.y) || min(c.x, d.x) > max(a.x, b.x) || min(c.y, d.y) > max(a.y, b.y))
return 0;
double h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
double i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
double j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
double k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}
bool cross(Line &p, Line &q)
{
return inter(p.a, p.b, q.a, q.b);
}
void make(Line &l)
{
for (vector<int>::iterator i = stk.begin(); i < stk.end();)
{
if (cross(line[*i], l))
i = stk.erase(i);
else
i++;
}
}
void work()
{
for (int i = 0; i < n; i++)
{
make(line[i]);
stk.push_back(i);
}
printf("Top sticks: %d", stk[0] + 1);
for (vector<int>::iterator i = stk.begin() + 1; i < stk.end(); i++)
printf(", %d", (*i) + 1);
printf(".\n");
}
int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
{
stk.clear();
input();
work();
}
return 0;
}