UVaLive 3905 Meteor (扫描线)

题意:给定上一个矩形照相机和 n 个流星,问你照相机最多能拍到多少个流星。

析:直接看,似乎很难解决,我们换一个思路,我们认为流星的轨迹就没有用的,我们可以记录每个流星每个流星在照相机中出现的时间段,

然后我们可以枚举时间段么?不行,这个是实数集上的,所以我们用扫描线,就相当于在x轴上有n个区间,我们从左到右拿一个竖线来扫描,

如果找到一个左端点,那么就加1,找到一个右端点就减1,注意,我们在排序时,要先按左端点排,再按右端点,不断更新答案即可,

并用我们可以避免使用浮点数。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
    int x, id;
    Node() { }
    Node(int xx, int i) : x(xx), id(i) { }
    bool operator < (const Node &p) const{
        return x < p.x || (x == p.x && id > p.id);
    }
};
Node a[maxn<<1];
int l, r;

void update(int x, int a, int w){
    if(!a){
        if(x <= 0 || x >= w)  r = l - 1;
    }
    else if(a > 0){
        l = max(l, -x * 2520 / a);
        r = min(r, (w - x) * 2520 / a);
    }
    else{
        l = max(l, (w - x) * 2520 / a);
        r = min(r, -x * 2520 / a);
    }
}

int main(){
    int T;  cin >> T;
    while(T--){
        int w, h;
        int cnt = 0;
        scanf("%d %d", &w, &h);
        int x, y, s, t;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            scanf("%d %d %d %d", &x, &y, &s, &t);
            l = 0, r = 1e9;
            update(x, s, w);
            update(y, t, h);
            if(l < r){
                a[cnt++] = Node(l, 0);
                a[cnt++] = Node(r, 1);
            }
        }
        sort(a, a + cnt);
        int ans = 0;
        int num = 0;
        for(int i = 0; i < cnt; ++i){
            if(!a[i].id)  ++num;
            else --num;
            ans = max(ans, num);
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/6516413.html