http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1843
http://poj.org/problem?id=2653
思路:
枚举即可,注意是“任意时刻”没有超过1000个top sticks,要不然最坏复杂度就是O(n^2)了。
完整代码:
/*UVa: 0.148s*/
/*POJ: 485ms,3942KB*/
#include<cstdio>
int ans[1005];
struct point
{
double x, y;
point(double x = 0, double y = 0): x(x), y(y) {}
} p1[100005], p2[100005];
inline double cross_product(point p1, point p2, point p)
{
return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);
}
bool is_intersect(point p1, point p2, point pp1, point pp2)
{
return cross_product(p1, p2, pp1) * cross_product(p1, p2, pp2) < 0 && cross_product(pp1, pp2, p1) * cross_product(pp1, pp2, p2) < 0;
}
int main()
{
int n, i, j, sum;
while (scanf("%d", &n), n)
{
for (i = 1; i <= n; ++i) scanf("%lf%lf%lf%lf", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y);
sum = 0;
for (i = 1; i < n; ++i)
{
for (j = i + 1; j <= n; ++j)
if (is_intersect(p1[i], p2[i], p1[j], p2[j])) break;
if (j > n) ans[sum++] = i;
}
printf("Top sticks: ");
for (i = 0; i < sum; ++i) printf("%d, ", ans[i]);
printf("%d.\n", n);
}
return 0;
}