本文主要是介绍POJ 2653 Pick-up sticks 线段交,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接暴力即可,因为题目中说了top line不超过1000个。
所以理论复杂度不会非常大
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#include <sstream>
#include <queue>
#include <vector>
#define MAXN 100005
#define MAXM 211111
#define eps 1e-8
#define INF 500000001
using namespace std;
inline int dblcmp(double d)
{if(fabs(d) < eps) return 0;return d > eps ? 1 : -1;
}
struct point
{double x, y;point(){}point(double _x, double _y): x(_x), y(_y) {}void input(){scanf("%lf%lf", &x, &y);}bool operator ==(point a)const{return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0;}point sub(point p){return point(x - p.x, y - p.y);}double dot(point p){return x * p.x + y * p.y;}double det(point p){return x * p.y - y * p.x;}double distance(point p){return hypot(x - p.x, y - p.y);}
};
struct line
{point a, b;line(){}line(point _a, point _b){ a = _a; b = _b;}void input(){a.input();b.input();}int segcrossseg(line v){int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a)));int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a)));if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2)return 2;return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0||d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0||d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0||d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0);}}seg[MAXN];
int n;
int ans[MAXN];
int main()
{while(scanf("%d", &n) != EOF && n){for(int i = 1; i <= n; i++) seg[i].input();int cnt = 0;ans[cnt++] = n;for(int i = n - 1; i >= 1; i--){int flag = 1;for(int j = i + 1; j <= n; j++)if(seg[i].segcrossseg(seg[j])){flag = 0;break;}if(flag) ans[cnt++] = i;}printf("Top sticks:");for(int i = cnt - 1; i > 0; i--) printf(" %d,", ans[i]);printf(" %d.\n", ans[0]);}return 0;
}
这篇关于POJ 2653 Pick-up sticks 线段交的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!