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 Native jni 编程入门
在某些情况下,java编程已经不能满足我们的需要,比如一个复杂的算法处理,这时候就需要用到jni(java native interface)技术: jni 其实就是java和c/cpp之间进行通信的 ...
- React Native实例
本文主要包括以下内容 View组件的实例 Text组件实例 Navigator组件实例 TextInput组件实例 View组件的实例 效果如下 代码如下 /** * Sample React Nat ...
- memarch
memached 是一个高性能的分布式对象缓存系统,用于动态web应用以减轻数库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态数据驱动网站的速度.memcached基于一个存储 ...
- jquery文件上传控件 Uploadify 问题记录
Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...
- MVC – 15.路由机制
15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...
- MVC – 4.mvc初体验(2)
5.显示学员列表 效果 数据表 5.1 首先,在文件夹Models新建一个新建项(W),选择ADO.NET 实体数据模型 (SingleTest.edmx) 5.2 建一个控制器,StudentsCo ...
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- xml解析方法总结
==========================================xml文件<?xml version=”1.0″ encoding=”GB2312″?> <RES ...
- UDP穿透NAT原理解析
转自:http://www.2cto.com/net/201201/116793.html NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益 ...
- 第四篇:SOUI资源文件组织
什么是资源? 现代的软件只要有UI,基本上少不了资源. 资源是什么?资源就是在程序运行时提供固定的数据源的文件. 在MFC当道的时代,资源一般就是位图(Bitmap),图标(Icon),光标(Curs ...