Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8862   Accepted: 3262

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.

Hint

Huge input,scanf is recommended.

Source


题意:
          依次给出n条线段,后添的线段若与先前的线段相交,则会覆盖先前的线段,问最后会有多少天线段,输出所能看见线段的下标。。。。。
          判断两条线段相交
思路:
         判断线段AB与线段CD是否相交;
         1:以线段AB,线段CD为对角线所构成的矩形相交,则继续判断;(用以排除两线段同线但不相交的情况)
         2:用叉积运算判断点A,B是否在线段CD两侧,判断点C,D是否在线段AB两侧,若符合,则两先段相交!
代码:
#include "cstdio"
#include "cmath"
#include "vector"
#include "iostream" using namespace std;
const double eps = 1e-8; double max(double a,double b){ return a>b?a:b; }
double min(double a,double b){ return a<b?a:b; } int cmp(double x){
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
} inline double sqr(double x){
return x*x;
} struct point{ //点结构体
double x,y;
point(){}
point (double a,double b):x(a),y(b) {} //重载
void input(){
scanf("%lf%lf",&x,&y);
}
friend point operator + (const point a,const point b){
return point(a.x+b.x,a.y+b.y);
}
friend point operator - (const point a,const point b){
return point(a.x-b.x,a.y-b.y);
}
}; double det(const point &a,const point &b){ //向量a与向量b的叉积
return a.x*b.y-a.y*b.x;
} struct line{ //线结构体
point a,b;
line(){}
line(point x,point y):a(x),b(y){}
}; bool line_make_point_one(line a,line b){ //判断两线段是否相交,完美代码!
return
max(a.a.x,a.b.x) >= min(b.a.x,b.b.x) && //前四行判断两向量所形成的矩形是否相交,排除两线段在同一条直线但不相交的可能
max(b.a.x,b.b.x) >= min(a.a.x,a.b.x) &&
max(a.a.y,a.b.y) >= min(b.a.y,b.b.y) &&
max(b.a.y,b.b.y) >= min(a.a.y,a.b.y) &&
cmp(det(a.a-b.b,b.a-b.b))*cmp(det(a.b-b.b,b.a-b.b))<=0 && //判断两线段是否相交
cmp(det(b.a-a.a,a.b-a.a))*cmp(det(b.b-a.a,a.b-a.a))<=0;
} int main(){
int n;
while(scanf("%d",&n),n!=0)
{
line a;
vector<line> p; //线段向量
vector<int> v; //记录线段向量的下标
p.clear();
v.clear(); scanf("%lf %lf %lf %lf",&a.a.x,&a.a.y,&a.b.x,&a.b.y);
p.push_back(a);
v.push_back(1);
for(int k=2;k<=n;++k)
{
scanf("%lf %lf %lf %lf",&a.a.x,&a.a.y,&a.b.x,&a.b.y);
for(int i=0; i<(int)p.size(); ++i)
{
bool flag = line_make_point_one(a,p[i]);
if(flag==true)
{
p.erase(p.begin()+i);
v.erase(v.begin()+i);
i--;
}
}
p.push_back(a);
v.push_back(k);
}
printf("Top sticks:");
int i;
for( i=0; i<(int)v.size()-1; ++i)
printf(" %d,",v[i]);
printf(" %d.\n",v[i]);
}
return 0;
}

计算几何--判断两条线段相交--poj 2653的更多相关文章

  1. Pick-up sticks(判断两条线段是否相交)

    Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8351 Accepted: 3068 Description Stan has ...

  2. [CSharpTips]判断两条线段是否相交

    判断两条线段是否相交 主要用到了通过向量积的正负判断两个向量位置关系 向量a×向量b(×为向量叉乘),若结果小于0,表示向量b在向量a的顺时针方向:若结果大于0,表示向量b在向量a的逆时针方向:若等于 ...

  3. 线段相交 POJ 2653

    // 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...

  4. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  5. c# 判断两条线段是否相交(判断地图多边形是否相交)

    private void button1_Click(object sender, EventArgs e) { //var result = intersect3(point1, point2, p ...

  6. 简单几何(线段相交) POJ 2653 Pick-up sticks

    题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...

  7. hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

  9. Jack Straws(判断线段是否相交 + 并查集)

    /** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840    题意:    判断线段 ...

随机推荐

  1. Winform开发框架之权限管理系统改进的经验总结(2)-用户选择界面的设计

    在上篇总结随笔<Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用>介绍了权限管理模块的用户管理部分,其中主要介绍了其中的用户所属公司 ...

  2. JavaScript异常捕获

    理论准备 ★   异常捕获 △ 异常:当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行: △ 异常抛出:当异常产生,并且这个异常生成一个错误信息: △ 异常捕获: ...

  3. Linux chmod命令详解

    Linux chmod命令详解 chmod----改变一个或多个文件的存取模式(mode)   chmod [options] mode files   只能文件属主或特权用户才能使用该功能来改变文件 ...

  4. Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs 前端主体结构(五)

    目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 6 Angularjs 前端主体结构 6.1 A ...

  5. 【GOF23设计模式】责任链模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_责任链模式.公文审批.供应链系统的采购审批.异常链.过滤器和拦截器调用过程 package com.test.chainO ...

  6. 基于MATLAB实现的云模型计算隶属度

    ”云”或者’云滴‘是云模型的基本单元,所谓云是指在其论域上的一个分布,可以用联合概率的形式(x, u)来表示 云模型用三个数据来表示其特征 期望:云滴在论域空间分布的期望,一般用符号Εx表示. 熵:不 ...

  7. Oracle SQL Tips

    左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...

  8. 在R语言环境中无法载入rJava包的解决办法

    问题描述: 安装包xlsx包后,运行library("xlsx")后弹出错误窗口: RGui (64-bit): Rgui.exe - 系统错误 无法启动此程序,因为计算机中丢失 ...

  9. android 开源和一些博客总结

    记录一些开源的android库 (1)Http请求库封装 https://github.com/kevinsawicki/http-request (2)浮动组件,定制化 toast http://f ...

  10. VS2015发布Webservice

    第一步:开启IIs:在控制面板程序——>程序功能——>打开或关闭windows功能,把“Internet信息服务”下面的“FTP服务器”.“Web管理工具”.“万维网服务”全部勾上,然后点 ...