线段相交 POJ 2653

时间:2023-03-08 23:35:30
线段相交 POJ 2653
 // 线段相交 POJ 2653
// 思路:数据比较水,据说n^2也可以过
// 我是每次枚举线段,和最上面的线段比较
// O(n*m) // #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const double inf = 123456789012345.0;
const LL MOD =100000000LL;
const int N =1e5+;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} int sgn(double x){
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;y = _y;
}
Point operator -(const Point &b)const{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const{
return x*b.x + y*b.y;
}
}; struct Line{
Point s,e;
int inx;
Line(){}
Line(Point _s,Point _e){
s=_s;e=_e;
}
// pair<int,Point> operator & (const Line &b) const{
// Point res=s;
// if(sgn((s-e)^(b.s-b.e))==0){
// if(sgn((s-b.e)^(b.s-b.e))==0)
// return make_pair(0,res);
// else return make_pair(1,res);
// }
// double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
// res.x+=(e.x-s.x)*t;
// res.y+=(e.y-s.y)*t;
// return make_pair(2,res);
// }
}; Line line[N];
bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= ;
} vector<Line> list;
bool cmp(Line l1,Line l2){
return l1.inx<l2.inx;
} int main(){
vector<Line>::iterator it;
int n;
while(~scanf("%d",&n),n){
list.clear();
for(int i=;i<=n;i++){
double x1,x2,y2,y1;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[i]=Line(Point(x1,y1),Point(x2,y2));
line[i].inx=i;
}
list.push_back(line[]);
for(int i=;i<=n;i++){
for(it=list.begin();it!=list.end();){
if(inter(line[i],*it)){
it=list.erase(it);
}
else it++;
}
list.push_back(line[i]);
}
printf("Top sticks: ");
sort(line+,line++list.size(),cmp);
for(it=list.begin();it!=list.end();it++){
if(it!=list.end()-){
printf("%d, ",(*it).inx);
}
else printf("%d.\n",(*it).inx);
}
}
return ;
}