Codeforces 954G:Castle Defense 二分

传送门

题目描述

一共有 n n n 面墙,初始有 a i a_i ai 个弓箭手在第 i i i 面墙的位置上。一个在 i i i 位置的弓箭手可以保护 ∣ i − j ∣ ≤ r |i - j| \leq r ijr 的所有墙 j j j

你现在可以增派 k k k 个弓箭手并且任意分配它们的位置。你需要最大化被数量最少的弓箭手保护的墙被弓箭手保护的数量。

n ≤ 5 × 1 0 5 , 0 ≤ r ≤ n , 0 ≤ k ≤ 1 0 18 , 0 ≤ a i ≤ 1 0 9 n \leq 5 \times 10^5, 0 \leq r \leq n, 0 \leq k \leq 10^{18} ,0 \leq a_i \leq 10^9 n5×105,0rn,0k1018,0ai109

分析

最大化最小值,首先想到的就是二分
我们去二分最终的答案,然后判断最后答案的可行性就可以了,需要注意的事如果某个位置不符合答案,需要在这个位置后面的d处安放一个弓箭手,因为这个位置前面已经满足条件了,不需要覆盖到

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 4e5 + 10,M = N * 2;
int h[N],ne[M],e[M],idx;
ll w[M];
ll d[N];
bool st[N];
int n,m;

void add(int x,int y,ll z){
    
    
    ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}

void dij(){
    
    
    d[0] = 0;
    priority_queue<PII,vector<PII>,greater<PII> > Q;
    Q.push(PII(0,0));
    while(Q.size()){
    
    
        PII p = Q.top();
        Q.pop();
        int t = p.second;
        if(st[t]) continue;
        st[t] = true;
        for(int i = h[t];i != -1;i = ne[i]){
    
    
            int j = e[i];
            if(d[j] > d[t] + w[i]){
    
    
                d[j] = d[t] + w[i];
                Q.push(PII(d[j],j));
            }
        }
    }
}

int main(){
    
    
    scanf("%d%d",&n,&m);
    for(int i = 0;i <= n;i++) h[i] = -1,d[i] = 0x3f3f3f3f3f3f;
    while(m--){
    
    
        int x,y;
        ll z;
        scanf("%d%d%lld",&x,&y,&z);
        add(x,y,2 * z),add(y,x,2 * z);
    }
    for(int i = 1;i <= n;i++){
    
    
        ll z;
        scanf("%lld",&z);
        add(0,i,z);
    }
    dij();
    for(int i = 1;i <= n;i++) printf("%lld ",d[i]);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/112548205