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. 求n阶方阵的值(递归)

    若有n*n阶行列式A,则: |A|=A[1][1]*M[1][1]+A[1][2]*M[1][2]+...A[1][n]*M[1][n]:其中M[1][i] 表示原矩阵元素A[1][i]的代数余子式: ...

  2. Quartus 11.0 的AS 下载方式和JTAG下载jic文件的方式

    FPGA下载的三种方式:主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式: AS由FPGA器件引导配置操作过程,它控制着外部存储器和初始化过程,EPCS系列.如EPCS1,EP ...

  3. wifi 4次握手

    转自:http://zhaoxiaobu.blog.51cto.com/878176/407130/ 不管是用WEP加密,还是用WPA,一般如果我们要和AP建立一个连接,要经过两个阶段认证(Authe ...

  4. Oracle如何写出高效的SQL

    转载:http://www.blogjava.net/ashutc/archive/2009/07/19/277215.html 1.选择最有效率的表明顺序(只在基于规则的优化器中有效) Oracle ...

  5. OCJP(1Z0-851) 模拟题分析(九)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  6. Codeforces Round #364 As Fast As Possible

    二分思想,对所要花费的时间进行二分,再以模拟的形式进行验证是否可行. 使用二分法,可以将一个求最优解的问题转化为一个判定问题,优雅的暴力. #include<cstdio> #includ ...

  7. js判断手机端Android手机还是iPhone手机

    /*判断当前设备是平板.安卓.苹果设备*/ <script type="text/javascript"> function fBrowserRedirect(){ v ...

  8. hdu 4293 2012成都赛区网络赛 dp ****

    题意:有n个人,可任意分成若干组,然后每个人个各提供一个信息,表示他们组前面有多少人,后面有多少人.问最多有多少个信息是不冲突的. 将n个人看成一组区间,然后每个人的信息可以表示为该人所在组的区间,然 ...

  9. 如果我可以重新学习iOS开发(转)

    在过去的几个月里,我一直在学习用Objective-C编写iOS app,最后我开始理清思绪.这比我想象中要难很多,也花了太长时间. 我经常遇到困难.感到沮丧,修复bug比实际写代码要花太多时间.但是 ...

  10. Android图像处理实例教程

    Android图像处理实例教程 原始出处 http://vaero.blog.51cto.com/4350852/856750