zoukankan      html  css  js  c++  java
  • Luogu 3479 [POI2009]GAS-Fire Extinguishers

    补上了这一道原题,感觉弱化版的要简单好多。

    神贪心:

    我们设$cov_{x, i}$表示在$x$的子树中与$x$距离为$i$的还没有被覆盖到的结点个数,设$rem_{x, i}$表示在$x$的子树中与$x$的距离为$i$的已经设立的消防站还能覆盖的点数。

    对于每一个$x$,我们考虑让它去管辖与它距离为$k$的点(即$cov_{x, k}$),因为这些点是必须由$x$来管辖的(即使在$x$的父亲处设立的消防站也管辖不到),容易知道需要这样的消防站的个数为$left lceil frac{cov_{x, k}}{s} ight ceil$。

    那么设立了这样的消防站之后$x$的$rem$肯定会有剩余,我们直接贪心地去用$rem$覆盖$cov$,如果想要使效果最大化,那么用$rem_{x, i}$来抵消$cov_{x, i/i - 1}$就好了,其他的让$x$的父亲来抵消,可以知道这样子答案一定不会变差。

    最后看看$1$号根节点有多少还不能覆盖的(设为$cnt$),答案加上$left lceil frac{cnt}{s} ight ceil$就好。

    注意$rem, cov, ans$乘起来会爆$int$。

    时间复杂度$O(nk)$。

    Code:

    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    
    const int N = 1e5 + 5;
    const int M = 25;
    
    int n, s, k, tot = 0, head[N];
    ll ans = 0LL, rem[N][M], cov[N][M];
    
    struct Edge {
        int to, nxt;
    } e[N << 1];
    
    inline void add(int from, int to) {
        e[++tot].to = to;
        e[tot].nxt = head[from];
        head[from] = tot;
    }  
    
    inline void read(int &X) {
        X = 0;
        char ch = 0;
        int op = 1;
        for(; ch > '9'|| ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op;
    }
    
    void dfs(int x, int fat) {
        cov[x][0] = 1;
        for(int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to;
            if(y == fat) continue;
            dfs(y, x);
            
            for(int j = 1; j <= k; j++) 
                cov[x][j] += cov[y][j - 1];
            for(int j = k; j >= 1; j--)
                rem[x][j - 1] += rem[y][j];
        }
        
        ll now = (cov[x][k] + s - 1) / s;
        ans += now, rem[x][k] += now * s;
        
        for(int i = 0; i <= k; i++) {
            if(!rem[x][i]) continue;
            for(int j = i; j >= 0 && (j >= i - 1 || x == 1); j--) {
                if(cov[x][j] >= rem[x][i]) {
                    cov[x][j] -= rem[x][i];
                    rem[x][i] = 0;
                    break;
                }
                rem[x][i] -= cov[x][j];
                cov[x][j] = 0;
            }
        }
    }
    
    int main() {
        read(n), read(s), read(k);
        for(int x, y, i = 1; i < n; i++) {
            read(x), read(y);
            add(x, y), add(y, x);
        }
        
        dfs(1, 0);
        
        ll now = 0;
        for(int i = 0; i <= k; i++)
            now += cov[1][i];
        ans += (now + s - 1) / s;
        
        printf("%lld
    ", ans);
        return 0; 
    }
    View Code
  • 相关阅读:
    ThinkPHP模版验证要注意的地方
    js关闭子页面刷新父页面
    js替换字符指定字符方法
    Ubuntu安装后的一些配置
    Docker入门
    RabbitMQ 基本概念和使用
    JAX-WS注解
    Linux 常用命令
    ubuntu16.04 搭建 Mysql服务器
    ubuntu 安装 Tomcat
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9540467.html
Copyright © 2011-2022 走看看