博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1147:Pick-up sticks(线段相交)
阅读量:6251 次
发布时间:2019-06-22

本文共 2898 字,大约阅读时间需要 9 分钟。

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3114    Accepted Submission(s): 1170


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.
 

Source
题意:给n跟筷子,按顺序放到地上,输出最后在最上面的筷子编号。

思路:每根筷子做一次线段相交判断即可。

# include 
# include
# include
# include
# include
using namespace std;const int maxn = 1e5;struct node{ double x, y;}a[maxn+3], b[maxn+3];int n, m, c[maxn+3];double cha(node a, node b, node c)//向量叉乘。{ return a.x*b.y + b.x*c.y + c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y;}bool fun(node a, node b, node c, node d){ if((max(a.x, b.x)>=min(c.x, d.x) || min(a.x, b.x)<=max(c.x, d.x)) && (max(a.y, b.y)>=min(c.y, d.y) || min(a.y, b.y)<=max(c.y, d.y)))//快速排斥。 { if(cha(a, b, c)*cha(a, b, d) <= 0 && cha(c, d, a)*cha(c, d, b)<=0) return true; else return false; } else return false;}int main(){ while(~scanf("%d",&n),n) { m = n; memset(c, 0, sizeof(c)); for(int i=1; i<=n; ++i) scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y); for(int i=1; i<=n; ++i) for(int j=i+1; j<=n; ++j) if(fun(a[i], b[i], a[j], b[j])) { c[i] = 1; --m; break; } printf("Top sticks: "); for(int i=1; i<=n; ++i) if(!c[i]&&m) { --m; printf("%d%c%c",i,!m?'.':',', !m?'\n':' '); } } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6729882.html

你可能感兴趣的文章
【第三十章】 elk(1) - 第一种架构(最简架构)
查看>>
你真的知道自己每天都需要做什么吗?
查看>>
c2java select algorithm
查看>>
Java Runtime.exec
查看>>
神经网络第一步,手写数字识别的例子分享给大家
查看>>
MobX响应式编程库
查看>>
Gradle基本使用(1):安装、IDEA使用
查看>>
Linux查看用户及其权限管理
查看>>
Kentico中的skin.css的加载
查看>>
elasticsearch6.3.1 安装以及配置IK 使用
查看>>
闪聊的beta版推出了
查看>>
WCF光芒下的Web Service
查看>>
GnuPG笔记
查看>>
批处理常用命令总结2
查看>>
ubuntu双网卡bonding配置(转)
查看>>
Ubuntu 14.04 关于 TensorFlow 环境的配置
查看>>
漂亮灵活设置的jquery通知提示插件toastr
查看>>
java多线程系类:基础篇:08之join
查看>>
TableView编辑状态下跳转页面的崩溃处理
查看>>
c#.net常用的小函数和方法集
查看>>