Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12861   Accepted: 4847

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^2啊???
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e5+;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}
struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return x<a.x||(x==a.x&&y<a.y);
}
void print(){
printf("%lf %lf\n",x,y);
}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;} double Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double Dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double DisPP(Point a,Point b){
Point t=a-b;
return sqrt(t.x*t.x+t.y*t.y);
}
struct Line{
Point s,t;
Line(){}
Line(Point p,Point v):s(p),t(v){}
}l[N];
bool isLSI(Line l1,Line l2){
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
bool isSSI(Line l1,Line l2){
Vector v1=l1.t-l1.s,v2=l2.t-l2.s;
if(sgn(Cross(v1,v2))==){
int flag=;
Vector u=l2.s-l1.s,w=l2.t-l1.s;
if(sgn(Dot(u,w))<) flag=;
u=l2.s-l1.t,w=l2.t-l1.t;
if(sgn(Dot(u,w))<) flag=;
return flag;
}
else return isLSI(l1,l2)&&isLSI(l2,l1);
} int n;
bool vis[N];
double x,y,x2,y2;
int main(int argc, const char * argv[]) {
while(true){
memset(vis,,sizeof(vis));
n=read(); if(n==) break;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
l[i]=Line(Point(x,y),Point(x2,y2));
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++) if(isSSI(l[j],l[i])){vis[i]=;break;}
}
printf("Top sticks: ");
int fir=;
for(int i=;i<=n;i++) if(!vis[i]){
if(fir) printf("%d",i),fir=;
else printf(", %d",i);
}
puts(".");
}
return ;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e5+;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}
struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return x<a.x||(x==a.x&&y<a.y);
}
void print(){
printf("%lf %lf\n",x,y);
}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;} double Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double Dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double DisPP(Point a,Point b){
Point t=a-b;
return sqrt(t.x*t.x+t.y*t.y);
}
struct Line{
Point s,t;
Line(){}
Line(Point p,Point v):s(p),t(v){}
}l[N];
bool isLSI(Line l1,Line l2){
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
bool isSSI(Line l1,Line l2){
Vector v1=l1.t-l1.s,v2=l2.t-l2.s;
if(sgn(Cross(v1,v2))==){
int flag=;
Vector u=l2.s-l1.s,w=l2.t-l1.s;
if(sgn(Dot(u,w))<) flag=;
u=l2.s-l1.t,w=l2.t-l1.t;
if(sgn(Dot(u,w))<) flag=;
return flag;
}
else return isLSI(l1,l2)&&isLSI(l2,l1);
} int n,st[N],top;
inline void del(int p){
for(int i=p;i<=top;i++) st[i]=st[i+];top--;
}
double x,y,x2,y2;
int main(int argc, const char * argv[]) {
while(true){
top=;
n=read(); if(n==) break;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
l[i]=Line(Point(x,y),Point(x2,y2));
for(int j=;j<=top;j++) if(isSSI(l[st[j]],l[i])) del(j),j--;
st[++top]=i;
}
printf("Top sticks: %d",st[]);
for(int i=;i<=top;i++) printf(", %d",st[i]);
puts(".");
}
return ;
}

POJ 2653 Pick-up sticks [线段相交 迷之暴力]的更多相关文章

  1. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  2. POJ 2653 Pick-up sticks(线段相交)

    题意:给定n个木棍依次放下,要求最终判断没被覆盖的木棍是哪些. 思路:快速排斥以及跨立实验可以判断线段相交. #include<algorithm> #include<cstdio& ...

  3. POJ 1066 Treasure Hunt (线段相交)

    题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...

  4. POJ 1410 Intersection --几何,线段相交

    题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...

  5. POJ 1269 Intersecting Lines(线段相交,水题)

    id=1269" rel="nofollow">Intersecting Lines 大意:给你两条直线的坐标,推断两条直线是否共线.平行.相交.若相交.求出交点. ...

  6. POJ 1066 Treasure Hunt【线段相交】

    思路:枚举四边墙的门的中点,与终点连成一条线段,判断与其相交的线段的个数.最小的加一即为答案. 我是傻逼,一个数组越界调了两个小时. #include<stdio.h> #include& ...

  7. poj 1556 (Dijkstra + Geometry 线段相交)

    链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  8. POJ 3304 Segments[直线与线段相交]

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  9. POJ 1408 Fishnet【枚举+线段相交+叉积求面积】

    题目: http://poj.org/problem?id=1408 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

随机推荐

  1. Oracle:对用户的CREATE、ALTER、GRANT、REVOKE操作练习

    --创建一个用户yong2,yong2的表空间为users,临时表空间为temp,users的表空间大小为10M,密码立刻过期,用户锁定. CREATE USER yong2IDENTIFIED BY ...

  2. css background之设置图片为背景技巧

    css background之设置图片为背景技巧-css 背景 Background是什么意思,翻译过来有背景意思.同样在css里面作为css属性一成员同样是有背景意思,并且是设置背景图片.背景颜色. ...

  3. 阿里云邮件服务器怎么设置才能在QQ邮箱访问,互发邮件?

    必须要在阿里云邮上打开IMAP和SMTP IMAP能够代发代收.在线更改.垃圾拦截,比POP3好: 记住打开的协议号: IMAP:143 带SSL:993 SMTP: 25 带SSL:465 前提是能 ...

  4. ngRx 官方示例分析 - 1. 介绍

    ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book  API  来查询图书,并保存自己的精选书籍列表. 菜单有两 ...

  5. CSS容器属性

    最近一直想美化博客的文字效果和增加文章末尾的转发提示,所以这两天抽空研究了一下CSS,前两篇可以翻我的博客,今天写的这篇是介绍增加文章末尾的转发提示,效果如文章末尾所示,好了,CSS很简单,我就不介绍 ...

  6. asp.net -mvc框架复习(8)-实现用户登录模型部分的编写

    1.配置文件添加数据库连接字符串(web.config) 2.编写通用数据库访问类 (1)引入命名空间 using System.Configuration; (2) 定义连接字符串 (3)编写完成 ...

  7. forward和redirect

    Forward和Redirect代表了两种请求转发方式:直接转发和间接转发. 直接转发方式(Forward),客户端和浏览器只发出一次请求,Servlet.HTML.JSP或其它信息资源,由第二个信息 ...

  8. 苹果内购服务器验证之receipt返回多组in_app思考

    最近有部分用户反映,苹果内购充值失败,经过测试总结有几个关键点出现问题 1.app购买成功苹果没有返回票据,属于票据遗漏(取决于苹果服务器的响应状况),只能客户端进行监听刷新等处理 2.app连续购买 ...

  9. 2017-06-20 (pwd ls cd)

    pwd pwd   显示当前所在的位置 pwd -P  如果是链接文件,显示链接文件所指的位置 ls ls 查询目录中的内容 ls  -a 显示所有的文件,包含隐藏的文件   -l 显示详细的信息   ...

  10. java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org.eclipse.wst.server.core\tmp7\wtpwebapps\Blog

    java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org. ...