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. Mysql手册—SQLStatementSyntax

    14.1.1 ALTER DATABASE Syntax,可用于修改数据库字符集和校验规则 查看校验规则可如下: 由于utf8的校验规则都是ci(case insensitive),所以是不区分大小写 ...

  2. Android 中的缓存机制与实现

    Android开发本质上就是手机和互联网中的web服务器之间进行通信,就必然需要从服务端获取数据,而反复通过网络获取数据是比较耗时的,特别是访问比较多的时候,会极大影响了性能,Android中可通过二 ...

  3. VB 笔记

    阅读方法:网页放大至200%,调整合适位置,阅读很方便,                                                                 csdn就可以 ...

  4. MVC - 16.MVC过滤器

          filter n. 滤波器:[化工] 过滤器:筛选:滤光器 vt. 过滤:渗透:用过滤法除去   1.过滤器表   过滤器类型 接口 默认实现 描述 Action IActionFilte ...

  5. 并发中的Native方法,CAS操作与ABA问题

    Native方法,Unsafe与CAS操作 >>JNI和Native方法 Java中,通过JNI(Java Native Interface,java本地接口)来实现本地化,访问操作系统底 ...

  6. windows 10 上office2016 word崩溃的解决方案

    方案1:--个人试验可行,把如下路径改成自己系统路径 C:Documents and Settings/Administrator/application/data/microsoft/templat ...

  7. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  8. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

  9. nexus私有仓库搭建

    步骤: 下载安装JDK(注意可用版本) .查看CentOS自带JDK是否已安装,输入: yum list installed |grep java 一般来说,如果是新装CentOS系统的话,不会有JD ...

  10. WCF学习笔记之WCF初识

    这篇博客将介绍WCF的最基础内容,让我们对WCF有一个基本的认识.后续的博客中将会介绍WCF其他方面内容.本篇博客将通过一个简单的例子,介绍如何创建WCF服务,并承载这个服务,让客户端来访问它.下面请 ...