• 线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)


    题目链接

    题意:

      给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点。

    思路:

      这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线。首先把所有点移到第一象限([0, 40000]),每个点从左到右排序,每个点在Y轴看成[y,y+H]的线段,扫描线从左到右扫描,把这条线段在线段树上+1,表示进矩形框,然后扫到这点对应的(x+W, y)的位置就-1,表示出了这个矩形框。那么线段树维护连续区间的线段的最大重叠数,即能覆盖到点的最大数量。

    #include <bits/stdc++.h>
    
    const int N = 1e4 + 5;
    struct Point {
        int x, y, z;
    };
    Point point[N<<1];
    
    bool cmp(const Point &a, const Point &b) {
        return a.x == b.x ? a.z > b.z : a.x < b.x;
    }
    
    #define lson l, mid, o << 1
    #define rson mid + 1, r, o << 1 | 1
    const int H = 40000 + 5;
    int sum[H<<2], lazy[H<<2];
    
    void push_up(int o) {
        sum[o] = std::max (sum[o<<1], sum[o<<1|1]);
    }
    
    void push_down(int o) {
        if (lazy[o]) {
            lazy[o<<1] += lazy[o];
            lazy[o<<1|1] += lazy[o];
            sum[o<<1] += lazy[o];
            sum[o<<1|1] += lazy[o];
            lazy[o] = 0;
        }
    }
    
    void build(int l, int r, int o) {
        sum[o] = 0; lazy[o] = 0;
        if (l == r) {
            return ;
        }
        int mid = l + r >> 1;
        build (lson);
        build (rson);
    }
    
    void updata(int ql, int qr, int c, int l, int r, int o) {
        if (ql <= l && r <= qr) {
            sum[o] += c; lazy[o] += c;
            return ;
        }
        push_down (o);
        int mid = l + r >> 1;
        if (ql <= mid) {
            updata (ql, qr, c, lson);
        }
        if (qr > mid) {
            updata (ql, qr, c, rson);
        }
        push_up (o);
    }
    
    int main() {
        int n, w, h;
        while (scanf ("%d", &n) == 1 && n > 0) {
            scanf ("%d%d", &w, &h);
            int tot = 0;
            for (int i=0; i<n; ++i) {
                int x, y;
                scanf ("%d%d", &x, &y);
                x += 20000; y += 20000;
                point[tot].x = x; point[tot].y = y;
                point[tot++].z = 1;
                point[tot].x = x + w; point[tot].y = y;
                point[tot++].z = -1;
            }
            std::sort (point, point+tot, cmp);
            build (0, 40000, 1);
            int ans = 0;
            for (int i=0; i<tot; ++i) {
                int ql = point[i].y, qr = point[i].y + h;
                if (qr > 40000) qr = 40000;
                updata (ql, qr, point[i].z, 0, 40000, 1);
                ans = std::max (ans, sum[1]);
            }
            printf ("%d
    ", ans);
        }
        return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    Linux命令大全
    paramiko 使用总结(SSH 操作远端机器)
    Django之ModelForm详解
    django模板之forloop
    学习VUE笔记及遇到的坑
    bootstrap table加载失败
    使用RedisTemplate遇到的坑
    grunt 不是内部或外部命令,也不是可运行的程序或批处理文件
    SpringBoot关于系统之间的远程互相调用
    数据在网络中的传输
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4523054.html
Copyright © 2020-2023  润新知