POJ2653 Pick-up sticks 判断线段相交

POJ2653

判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的

题目保证最后答案小于1000

故从后往前尝试用后面的线段 "压"前面的线段 排除不可能的答案 就可以轻松AC了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

const double eps=1e-9;

int cmp(double x)
{
 if(fabs(x)<eps)return 0;
 if(x>0)return 1;
 	else return -1;
}

const double pi=acos(-1.0);

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);
	}
 friend bool operator ==(const point &a,const point &b)
 	{
 	 return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
	}
 friend point operator *(const point &a,const double &b)
 	{
 	 return point(a.x*b,a.y*b);
	}
 friend point operator*(const double &a,const point &b)
 	{
 	 return point(a*b.x,a*b.y);
	}
 friend point operator /(const point &a,const double &b)
 	{
 	 return point(a.x/b,a.y/b);
	}
 double norm()
 	{
 	 return sqrt(sqr(x)+sqr(y));
	}
};

double det(const point &a,const point &b)
{
 return a.x*b.y-a.y*b.x;
}

double dot(const point &a,const point &b)
{
 return a.x*b.x+a.y*b.y;
}

double dist(const point &a,const point &b)
{
 return (a-b).norm();
}

point rotate_point(const point &p,double A)
{
 double tx=p.x,ty=p.y;
 return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}

struct line
{
 point a,b;
 line(){};
 line(point x,point y):a(x),b(y)
 {

 }
};

bool parallel(line a,line b)
{
 return !cmp(det(a.a-a.b,b.a-b.b));
}

bool line_joined(line a,line b,point &res)
{
 if(parallel(a,b))return false;
 double s1=det(a.a-b.a,b.b-b.a);
 double s2=det(a.b-b.a,b.b-b.a);
 res=(s1*a.b-s2*a.a)/(s1-s2);
 return true;
}

bool pointonSegment(point p,point s,point t)
{
 return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
}

const int maxn=100000+1;
line li[maxn];
deque<int>q;
deque<int>qq;
int main()
{freopen("t.txt","r",stdin);
 //freopen("1.txt","w",stdout);
 int n;
 while(scanf("%d",&n))
 	{
 	 while(!q.empty())q.pop_front();
 	 while(!qq.empty())qq.pop_front();
 	 if(n==0)return 0;
 	 for(int i=1;i<=n;i++)
 	 	{
 	 	 point a,b;
 	 	 a.input();b.input();
 	 	 li[i]=line(a,b);

		 q.push_back(i);
		}
	 for(int i=n;i>1;i--)
	 	{
	 	  while(!q.empty())
 	 	 	{

 	 	 	 int nv=q.front();
 	 	 	 if(nv>=i)break;
 	 	 	 line nl=li[nv];q.pop_front();
 	 	 	 point jo;
 	 	 	 if(!line_joined(nl,li[i],jo)){qq.push_back(nv);continue;}
 	 	 	 if((pointonSegment(jo,nl.a,nl.b))&&(pointonSegment(jo,li[i].a,li[i].b)))continue;
			 qq.push_back(nv);
			}
		 while(!qq.empty())
		 	{
			  int nv=qq.back();qq.pop_back();
			  q.push_front(nv);
			}
		}
	 printf("Top sticks: ");
	 while(!q.empty())
	 	{
	 	 int now=q.front();q.pop_front();
	 	 if(q.empty())printf("%d.",now);
	 	 	else printf("%d, ",now);
		}
	 printf("\n");
	}
 return 0;
}

  

时间: 2024-10-07 05:46:13

POJ2653 Pick-up sticks 判断线段相交的相关文章

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

一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 100005 using namespace std; struct Point { double x

POJ 2653 Pick-up sticks (判断线段相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 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 fin

hdu 1147 Pick-up sticks 判断线段相交 ~~ 注意判断顺序!!不然容易超时

Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2216    Accepted Submission(s): 815 Problem Description Stan has n sticks of various length. He throws them one at a time on the f

POJ_2653_Pick-up sticks_判断线段相交

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

hdu 1086(判断线段相交)

传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点与 另一条线段的两个端点构成的两个向量求叉积,如果线段相交那么另一条线段两个端点必定在该线段的两边,则该线段代表的向量必定会顺时针转一遍逆时针转一遍,叉积必定会小于等于0,同样对另一条线段这样判断一次即可. #include <algorithm> #include <cstdio>

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

HDU1558 - Segment set 并查集 + 判断线段相交

HDU1558 - Segment set: http://acm.hdu.edu.cn/showproblem.php?pid=1558 题目大意: 输入一些线段的端点坐标,若两线段相交,则把他们合并成一个集合,输入数据里会有k,问和k线段相交的线段的数目(包括自己) 思路: 每次输入一条线段,都从头扫描一次. 找出之前输入的线段里面,所有和其相交的线段,并且合并(合并用的是线段的ID). 就是: 并查集 + 判断线段相交 代码: #include <iostream> #include &

POJ 2826 An Easy Problem? 判断线段相交

POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double eps = 1e-8;

fzu 1015 土地划分(判断线段相交+求出交点+找规律)

链接:http://acm.fzu.edu.cn/problem.php?pid=1015  Problem 1015 土地划分 Accept: 714    Submit: 1675Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 在Dukeswood这块土地上生活着一个富有的农庄主和他的几个孩子.在他临终时,他想把他的土地分给他的孩子.他有许多农场,每个农场都是一块矩形土地.他在农场地图上划上一些直线将