POJ 2653 Pick-up sticks
来源:互联网 发布:足球竞猜软件 编辑:程序博客网 时间:2024/05/19 22:24
链接:http://poj.org/problem?id=2653
题目:
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 9127 Accepted: 3367
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
51 1 4 22 3 3 11 -2.0 8 41 4 8 23 3 6 -2.030 0 1 11 0 2 12 0 3 10
Sample Output
Top sticks: 2, 4, 5.Top sticks: 1, 2, 3.
解题思路:
按从前往后的递增顺序记录下每条线段,从前往后遍历(对于线段来讲,就是从下往上遍历),如果没有线段与其相交(即这条线段上面没有覆盖其他线段),那么这条线段就是最上面的线段。算法时间复杂度为O(n2)。这里最关键的地方就是判断两条线段是否相交,也就是要判断两条线段是否互相跨立,对于这个问题,我们可以利用叉积的性质来解,线段AB,线段CD,如果(向量AB^(叉积)向量AC) *( 向量AB^(叉积)向量AD) < 0, 表明C、D两点对线段AB跨立,= 0,表明C、D中有一点在AB所在的直线上, > 0,表明C、D两点在线段AB同侧。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 100010;struct Segment{double sx, sy, ex, ey;};Segment Seg[MAXN];double fun(double x1, double y1, double x2, double y2){return x1 * y2 - x2 * y1;}bool isIntersect(Segment a, Segment b){if(fun(a.ex - a.sx, a.ey - a.sy, b.sx - a.sx, b.sy - a.sy) * fun(a.ex - a.sx, a.ey - a.sy, b.ex - a.sx, b.ey - a.sy) <= 0 && fun(b.ex - b.sx, b.ey - b.sy, a.sx - b.sx, a.sy - b.sy) * fun(b.ex - b.sx, b.ey - b.sy, a.ex - b.sx, a.ey - b.sy) <= 0) return true;return false;}int main(){int n;while(~scanf("%d", &n) && n){memset(Seg, 0, sizeof(Seg));for(int i = 0; i < n; i++){scanf("%lf%lf%lf%lf", &Seg[i].sx, &Seg[i].sy, &Seg[i].ex, &Seg[i].ey);}printf("Top sticks:");bool first = true;for(int i = 0; i < n; i++){bool flag = true;for(int j = i + 1; j < n; j++){if(isIntersect(Seg[i], Seg[j])){flag = false;break;}}if(flag){if(!first){printf(", %d", i + 1);}else{printf(" %d", i + 1);first = false;}}}puts(".");}return 0;}
0 0
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- POJ 2653 Pick-up sticks
- MAT Memory Analyzer Tool 插件安装 myeclipse10.1
- centos中mysql28000问题处理
- hdu1978 How many ways
- 使用colorbox进行弹窗遇到的怪事,关闭弹窗报错colorbox undefined
- Linux内核中ioremap映射的透彻理解
- POJ 2653 Pick-up sticks
- 数据仓库篇章 OLTP/OLAP 区别
- c#中处理关于SQL中BLOG大数据的方法
- linux网络编程-服务器端线程池技术(C语言实现)
- window下设置apache虚拟主机
- [noj 1561] Set Time
- Java语言中的值传递与引用传递
- poj 3177 Redundant Paths
- Nginx学习之十-超时管理(定时器事件)