Pick-up sticks
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2950 Accepted Submission(s): 1108

Problem 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.

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.

  虽然总的边数很多,但是他说了位于最上面的边不超过1000个。所以只要维护一个长度不超过1000的链表,从前往后计算。一开始链表为空,对于每一条边,将它与链表中的每个元素比较是否相交,若相交则将该边移除,比较完成后将当前边加入到链表末尾。全部处理结束后,剩下的边就是最上面的边。

#include <stdio.h>
#include <iostream>
#include <list>
using namespace std;
// ALGORITHM OF GEOMETRY -> struct Geometry_vertex {
double x, y;
};
struct Geometry_vector {
double x, y;
};
struct Geometry_line {
Geometry_vertex A, B;
int i;
};
Geometry_vector MakeGeometry_vector(Geometry_vertex v1, Geometry_vertex v2) {
Geometry_vector v;
v.x = v2.x - v1.x;
v.y = v2.y - v1.y;
return v;
}
double DotProduct(Geometry_vector v1, Geometry_vector v2) {
return ((v1.x) * (v2.x) + (v1.y) * (v2.y));
}
double CrossProduct(Geometry_vector v1, Geometry_vector v2) {
return ((v1.x) * (v2.y) - (v2.x) * (v1.y));
}
bool IsLineCrossed(Geometry_line l1, Geometry_line l2) {
Geometry_vector v1, v2;
double c1, c2;
v1 = MakeGeometry_vector(l1.A, l1.B);
v2 = MakeGeometry_vector(l1.A, l2.A);
c1 = CrossProduct(v1, v2);
v2 = MakeGeometry_vector(l1.A, l2.B);
c2 = CrossProduct(v1, v2);
if(c1 * c2 >= ) {
return false;
}
v1 = MakeGeometry_vector(l2.A, l2.B);
v2 = MakeGeometry_vector(l2.A, l1.A);
c1 = CrossProduct(v1, v2);
v2 = MakeGeometry_vector(l2.A, l1.B);
c2 = CrossProduct(v1, v2);
if(c1 * c2 >= ) {
return false;
}
return true;
}
// <- ALGORITHM OF GEOMETRY
list<Geometry_line> G;
int main() {
int n;
while(scanf("%d", &n) != EOF) {
if(n == ) {
break;
}
G.clear();
Geometry_line l;
for(int i = ; i <= n; i++) {
scanf("%lf%lf%lf%lf", &l.A.x, &l.A.y, &l.B.x, &l.B.y);
l.i = i;
for(list<Geometry_line>::iterator it = G.begin(); it != G.end();) {
if(IsLineCrossed(*it, l)) {
it = G.erase(it);
} else {
it++;
}
}
G.push_back(l);
}
printf("Top sticks: ");
for(list<Geometry_line>::iterator it = G.begin(); it != G.end(); it++) {
list<Geometry_line>::iterator j = it;
j++;
if(j == G.end()) {
printf("%d.\n", (*it).i);
} else {
printf("%d, ", (*it).i);
}
}
}
return ;
}

Pick-up sticks[HDU1147]的更多相关文章

  1. The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543

    Pick The Sticks Time Limit: 15000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  2. 2015南阳CCPC D - Pick The Sticks dp

    D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...

  3. CDOJ 1218 Pick The Sticks

    Pick The Sticks Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  4. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

  5. UESTC 1218 Pick The Sticks

    Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Status ...

  6. hdu 5543 Pick The Sticks(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:给你一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上 ...

  7. DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)

    题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...

  8. [HDOJ5543]Pick The Sticks(DP,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...

  9. uestc oj 1218 Pick The Sticks (01背包变形)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...

随机推荐

  1. eclipse failed to create the java virtual machine 问题图文解析

    eclipse failed to create the java virtual machine 问题图文解析 分类: java常用软件异常2010-10-02 23:45 73200人阅读 评论( ...

  2. TCP/IP五层模型

    (2)TCP/IP五层模型的协议   应用层 传输层 网络层 数据链路层 物理层   物理层:中继器.集线器.还有我们通常说的双绞线也工作在物理层 数据链路层:网桥(现已很少使用).以太网交换机(二层 ...

  3. NYOJ_37.回文字符串 (附滚动数组)

    时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...

  4. PHP中 字符串 常用函数

    //strpos    查找字符串中是否含有字符 $str='abcde'; $char='a':   if(strpos($str,$char) !==false){    echo '含有',$c ...

  5. Mysql中文乱码问题完美解决方案

    drop database`netctoss_demo` ;CREATE DATABASE `netctoss_demo` CHARACTER SET 'utf8' COLLATE 'utf8_gen ...

  6. gdo图形引擎中的旋转角

    横滚角(Roll) bank.roll  绕y轴 z轴正向为起点逆时针方向:往左为正,往右为负,水平时为0:有效范围:-180度-180度 注:下图是从飞机的尾部-->头部方向观察所得 俯仰角( ...

  7. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 334人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  8. 【c++】必须在类初始化列表中初始化的几种情况

    转自:http://www.cnblogs.com/kaituorensheng/p/3477630.html 1. 类成员为const类型 2. 类成员为引用类型 #include <iost ...

  9. python学习-爬虫

    转载自 静觅的博客 最普通下载网页 import urrlib2 response = urllib2.urlopen("http://www.baidu.com") print ...

  10. 汇编学习(六)——代码转换程序

    (一)逻辑运算指令 一.双操作数逻辑运算指令 1.指令格式: AND dst,src ; "与"运算, OR dst,src ; "或"运算 XOR dst,s ...