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. java课程设计 学生管理系统

    学生成绩管理系统 可实现功能 学生管理系统 查询学生信息:姓名.学号.性别.出生年月日.(学号自动生成且唯一) 查询学生成绩:每个人都有数学.Java与体育与选修课 查询学生课程 进行公选课选课 教师 ...

  2. 怎么设置table(表格)手机端自适应宽度

    我们在wordpress文章页面经常会用到表格,有些是从其他网页或者文档复制粘贴过来的,在电脑设备网页上都能很好的展示,但是在移动设备上或者低分辨率设备上却经常撑破列宽,很是影响美观和客户体验.这里就 ...

  3. 正则表达式之旅_sed_awk

    谈谈正则表达式这个东西: 我想作为一个程序员,正则表达式大家绝对不陌生. 正则表达式好像一个有限则动机.主要作用是匹配,但是同时因为这个功能,我们可以扩展很多其他用法 像很多语言都引人了正则表达式:j ...

  4. could not read column value from result set:

    错误描述: INFO [http-apr-8080-exec-26] (NullableType.java:203) - could not read column value from result ...

  5. [洛谷P2711]小行星

    题目大意:有$n$颗行星,每颗行星的位置是$(x,y,z)$.每次可以消除一个面(即$x,y$或$z$坐标相等)的行星,求消除这些行星的最少次数. 题解:最小割,对于每一颗小行星,从 x 面的出点向  ...

  6. [Leetcode] word break 拆分词语

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. BZOJ5011 [JXOI2017]颜色 【线段树 + 主席树】

    题目链接 BZOJ5011 题解 一定只有我这种智障会用这么奇怪的方法做这道题.. 由题我们知道最后剩余的一定是一个区间,而且区间内的颜色不存在于区间外 所以我们的目的就是为了找到这样的区间的数量 区 ...

  8. 【NOIP模拟赛】书 数学+期望概率

    biubiu~~~ 对于这道傻题.........我考场上退了一个多小时才推出来这个东西是排列...........然后我打的dfs效率n!logInf正好n=9是最后一个能过的数结果前三个点的n全是 ...

  9. cookie 是存储于访问者的计算机中的变量

    今天把javascript如何用来创建及存储cookie复习了一下,其中的一点体会拿出来和大家讨论,首先看一下基础知识: 什么是cookie cookie 是存储于访问者的计算机中的变量.每当同一台计 ...

  10. Supermarket [堆]

    Supermarket 题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=\(10^4\))个商品,同时每一样商品都有收益Pi​ ,和过期时间Di​ (1<=Pi,Di&l ...