首页 > 代码库 > POJ 2653 Pick-up sticks(几何)
POJ 2653 Pick-up sticks(几何)
Pick-up sticks
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 13377 | Accepted: 5039 |
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.
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.
Source
Waterloo local 2005.09.17
题解:
几何模板题目
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 #define ms(a, b) memset(a, b, sizeof(a)) 15 #define pb push_back 16 #define mp make_pair 17 #define eps 0.0001 18 const LL INF = 0x7fffffff; 19 const int inf = 0x3f3f3f3f; 20 const int mod = 1e9+7; 21 const int maxn = 100000+10; 22 int n, cnt; 23 struct POINT 24 { 25 double x, y, z; 26 POINT():x(0), y(0), z(0){}; 27 POINT(double _x_, double _y_, double _z_ = 0):x(_x_), y(_y_), z(_z_) {}; 28 }; 29 struct SEG 30 { 31 POINT a; 32 POINT b; 33 SEG(){}; 34 SEG(POINT _a_, POINT _b_):a(_a_),b(_b_) {}; 35 }; 36 double Cross(const POINT &a, const POINT & b, const POINT &o)//叉乘 37 { 38 return (a.x - o.x)*(b.y - o.y) - (b.x - o.x)*(a.y - o.y); 39 } 40 bool IsIntersect(const SEG &u, const SEG &v) 41 { 42 return (Cross(v.a, u.b, u.a)*Cross(u.b, v.b, u.a)>=0)&& 43 (Cross(u.a, v.b, v.a)*Cross(v.b, u.b, v.a)>=0)&& 44 (max(u.a.x, u.b.x) >= min(v.a.x, v.b.x))&& 45 (max(v.a.x, v.b.x) >= min(u.a.x, u.b.x))&& 46 (max(u.a.y, u.b.y) >= min(v.a.y, v.b.y))&& 47 (max(v.a.y, v.b.y) >= min(u.a.y, u.b.y)); 48 } 49 int ans[maxn]; 50 SEG stick[maxn]; 51 void init() 52 { 53 cnt = 0; 54 } 55 void solve() { 56 double a, b, c, d; 57 for(int i = 1;i<=n;i++){ 58 scanf("%lf%lf%lf%lf", &a, &b, &c, &d); 59 stick[i].a.x=a;stick[i].a.y=b; 60 stick[i].b.x=c;stick[i].b.y=d; 61 } 62 for(int i = n;i>0;i--){ 63 bool flag = 1; 64 for(int j = i+1;j<=n;j++){ 65 if(IsIntersect(stick[i], stick[j])){ 66 flag = 0; 67 break; 68 } 69 } 70 if(flag){ 71 ans[cnt++] = i; 72 } 73 if(cnt>=1000){ 74 break; 75 } 76 } 77 printf("Top sticks:"); 78 for(int i = cnt-1;i>=0;i--){ 79 if(i==0) printf(" %d.",ans[i]); 80 else printf(" %d,",ans[i]); 81 } 82 printf("\n"); 83 } 84 int main() { 85 #ifdef LOCAL 86 freopen("input.txt", "r", stdin); 87 // freopen("output.txt", "w", stdout); 88 #endif 89 while(~scanf("%d", &n)&&n){ 90 init(); 91 solve(); 92 } 93 return 0; 94 }
POJ 2653 Pick-up sticks(几何)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。