LA-3905 (扫描线)

题意:

给一些流星的初始位置和运动向量,给了相机的拍摄范围;问你最多能拍到多少颗流星;

思路:

将流星用出现在相机拍摄范围内的时间段表示;sort后在扫面端点更新最大值;

Ac代码:

#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef  long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+10;
const int maxn=1005;
const double eps=1e-10;


double l,r;
struct Seg
{
    double po;
    int type;
}seg[2*N];
int cmp(Seg x,Seg y)
{
    if(x.po==y.po)return x.type<y.type;
    return x.po<y.po;
}
void ope(double b,double s,double v)
{
    if(v==0)
    {
        if(b<=0||b>=s)r=l-1;
    }
    else if(v>0)
    {
            l=max(l,-b/v);
            r=min(r,(s-b)/v);
    }
    else
    {
            l=max(l,(s-b)/v);
            r=min(r,-b/v);
    }
}
int main()
{
        int t;
        read(t);
        while(t--)
        {
            int n;
            double w,h;
            scanf("%lf%lf",&w,&h);
            read(n);
            int cnt=0;
            For(i,1,n)
            {
                l=0,r=1000000000;
                double x,y,a,b;
                scanf("%lf%lf%lf%lf",&x,&y,&a,&b);
                ope(x,w,a);
                ope(y,h,b);
                if(r<=l)continue;
                seg[++cnt].po=l;
                seg[cnt].type=1;
                seg[++cnt].po=r;
                seg[cnt].type=0;
            }
            sort(seg+1,seg+cnt+1,cmp);
            int ans=0,num=0;
            For(i,1,cnt)
            {
                if(seg[i].type)num++;
                else num--;
                ans=max(ans,num);
            }
            cout<<ans<<"
";
        }
        return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5663094.html