Shooter UVA - 10535 最大重叠区间

问题

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值