Pick-up sticks[HDU1147]
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]的更多相关文章
- 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 ...
- 2015南阳CCPC D - Pick The Sticks dp
D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...
- CDOJ 1218 Pick The Sticks
Pick The Sticks Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- 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 ...
- UESTC 1218 Pick The Sticks
Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- hdu 5543 Pick The Sticks(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:给你一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上 ...
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...
- [HDOJ5543]Pick The Sticks(DP,01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
随机推荐
- Android 图片闪烁(延迟切换)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- DB2 bind on z/os
BIND and REBIND options for packages and plans There are several options you can use for binding or ...
- 《Thinking in Java》十七章_容器深入研究_练习13(Page484)
练习13: 单词计数器 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFou ...
- GB2312、GBK和UTF-8三种编码以及QT中文显示乱码问题
1.GB2312.GBK和UTF-8三种编码的简要说明 GB2312.GBK和UTF-8都是一种字符编码,除此之外,还有好多字符编码.只是对于我们中国人的应用来说,用这三种编码 比较多.简单的说一下, ...
- 《C#本质论》读书笔记(12)委托和Lambda表达式
12.1.委托概述 12.1.2 委托的数据类型 为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法.此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型--也 ...
- jQuery – 7.动态创建Dom、删除节点
动态创建Dom节点 1.使用$(html字符串)来创建Dom节点 2.append方法用来在元素的末尾追加元素 案例:动态生成网站列表 3.prepend,在元素的开始 ...
- 理解Java中的引用传递和值传递
关于Java传参时是引用传递还是值传递,一直是一个讨论比较多的话题,有论坛说Java中只有值传递,也有些地方说引用传递和值传递都存在,比较容易让人迷惑.关于值传递和引用传递其实需要分情况看待,今天学习 ...
- HTTP中302与301的区别以及在ASP.NET中如何实现
一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently Moved).302 re ...
- 【PHP的异常处理【完整】】
PHP的异常处理机制大多数和java的很相似,但是没有finally,而且还可以自定义顶级异常处理器:捕捉到异常信息后,会跳出try-catch块,如果catch中没有跳转的动作,则会继续执行下一条语 ...
- DB2 日期相减
简单方法: 使用 days 字符型的日期:2012-01-01,2012-01-11 values days(date('2012-01-11')) - days(date('2012-01-01' ...