问题
https://vjudge.net/problem/UVA-10535
分析
最大重叠区间问题,和例题LA 3905一样,如果两个点重叠,左端点在前面(就是因为这一点,一直WA)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=500+5;
const double eps=1e-9;
struct Line{
double x1,y1,x2,y2;
Line(double x1=0, double y1=0, double x2=0, double y2=0):x1(x1),y1(y1),x2(x2),y2(y2){}
}line[maxn];
//0是左端点,1是右端点
struct Seg{
double p;
int type;
Seg(double p=0,int type=0):p(p),type(type){}
bool operator < (const Seg &rhs) const {
if(fabs(p-rhs.p)>eps) return p<rhs.p;
return type<rhs.type;
}
}seg[4*maxn];
int n;
double cx,cy;
int main(void){
while(scanf("%d",&n)==1 && n){
for(int i=0;i<n;++i){
scanf("%lf%lf%lf%lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
}
scanf("%lf%lf",&cx,&cy);
double l,r;
int cnt=0;
for(int i=0;i<n;++i){
l=atan2(line[i].y1-cy,line[i].x1-cx);
r=atan2(line[i].y2-cy,line[i].x2-cx);
if(l>r) swap(l,r);
//,射手在原点,线段穿过了x轴负半轴
if(r-l>M_PI){
seg[cnt++]=Seg(-M_PI,0);
seg[cnt++]=Seg(l,1);
seg[cnt++]=Seg(r,0);
seg[cnt++]=Seg(M_PI,1);
}else{
seg[cnt++]=Seg(l,0);
seg[cnt++]=Seg(r,1);
}
}
sort(seg,seg+cnt);
int ans=0,t=0;
for(int i=0;i<cnt;++i){
if(seg[i].type==0) {++t; ans=max(ans,t);}
else --t;
}
printf("%d\n",ans);
}
return 0;
}