Pick-up sticks

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

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.
 

题意:n根stick,从第一根开始扔到平面上,第i根有可能覆盖前1-(i-1)的某些stick,求最后还有多少stick没有被覆盖.

 
分析:判断线段是否相交即可。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
struct Point{
double x,y;
}p[*N]; ///叉积
double mult(Point a, Point b, Point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
} ///a, b为一条线段两端点c, d为另一条线段的两端点 相交返回true, 不相交返回false
bool isCross(Point a, Point b, Point c, Point d)
{
if (max(a.x,b.x)<min(c.x,d.x))return false;
if (max(a.y,b.y)<min(c.y,d.y))return false;
if (max(c.x,d.x)<min(a.x,b.x))return false;
if (max(c.y,d.y)<min(a.y,b.y))return false;
if (mult(c, b, a)*mult(b, d, a)<)return false;
if (mult(a, d, c)*mult(d, b, c)<)return false;
return true;
}
bool under[N]; ///记录哪些stick在下面
int ans[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF,n){
memset(under,false,sizeof(under));
int k=;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&p[k].x,&p[k].y,&p[k+].x,&p[k+].y);
k+=;
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(isCross(p[*i-],p[*i],p[*j-],p[*j])) {
under[i]=true;
break;
}
}
} printf("Top sticks: ");
int t=;
for(int i=;i<=n;i++){
if(!under[i]) ans[t++]=i;
}
for(int i=;i<t-;i++){
printf("%d, ",ans[i]);
}
printf("%d.\n",ans[t-]);
}
return ;
}

hdu 1147(线段相交)的更多相关文章

  1. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

  2. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  3. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 1147:Pick-up sticks(基本题,判断两线段相交)

    Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)

    题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...

  6. HDU 3492 (直线与所有线段相交) Segment

    题意: 给出n个线段,判断是否存在一条直线使得所有线段在直线上的射影的交非空. 分析: 如果我们找到一条与所有线段相交的直线,然后做一条与该直线垂直的直线,这些线段在直线上的射影就一定包含这个垂足. ...

  7. hdu 1086(判断线段相交)

    传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...

  8. hdu 1086 You can Solve a Geometry Problem too [线段相交]

    题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...

  9. hdu 3304(直线与线段相交)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12042   Accepted: 3808 Descrip ...

随机推荐

  1. Mybatis学习系列(四)Mapper接口动态代理

    实现原理及规范 Mapper接口动态代理的方式需要手动编写Mapper接口,Mybatis框架将根据接口定义创建接口的动态代理对象,代理对象的方法体实现Mapper接口中定义的方法. 使用Mapper ...

  2. Web-request内置对象在JSP编程中的应用

  3. 用Electron开发桌面应用app的相关文献集锦

    1. 超棒的发声器(项目实战) 原文点此链接 2. Electron中文文档 原文点此链接

  4. yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

    yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux   三种解决方案 我采取第三种方案解决的 第一种: linu ...

  5. POJ2699:The Maximum Number of Strong Kings(枚举+贪心+最大流)

    The Maximum Number of Strong Kings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2488 ...

  6. HDU1828 Picture 线段树+扫描线模板题

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. io流中的装饰模式对理解io流的重要性

    为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...

  8. java过滤器和监听器详解

    过滤器 1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请求 ...

  9. oracle 包和包实现

    包: create or replace package sp_pexam_clear as --定义结构体 /*type re_stu is record( rname student.name%t ...

  10. 转:A Painless Q-learning Tutorial (一个 Q-learning 算法的简明教程)

    demo 参见 MDP DEMO   本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文 ...