CodeForces954GCastle Defense(二分答案+差分)

Posted void_f

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces954GCastle Defense(二分答案+差分)相关的知识,希望对你有一定的参考价值。

Description

题目链接

Solution

二分答案,套一个差分标记即可

每次放弓箭手显然越右边越优

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2000010
#define ll long long
using namespace std;

int n,R;
ll A[N],k,Ans,l=1e18,r=1e20,cf[N],sum[N];

inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

bool check(ll m){
    ll tmp=k,x=0;
    memset(cf,0,sizeof(cf));
    for(int i=1;i<=n;++i){
        x+=cf[i];
        if(sum[min(i+R,n)]-sum[max(i-R-1,0)]+x<m){
            ll d=m-(sum[min(i+R,n)]-sum[max(i-R-1,0)]+x);
            if(d>tmp) return 0;
            tmp-=d;
            x+=d;
            cf[i+2*R+1]-=d;
        }
    }
    return 1;
}

int main(){
    scanf("%d%d%I64d\n",&n,&R,&k);
    for(int i=1;i<=n;++i){sum[i]=sum[i-1]+(A[i]=read());l=min(l,A[i]);}
    while(l<r){
        ll m=(l+r)>>1ll;
        if(check(m)) Ans=m,l=m+1;
        else r=m;
    }
    printf("%I64d\n",Ans);
    return 0;
}

以上是关于CodeForces954GCastle Defense(二分答案+差分)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 954 G. Castle Defense

CodeForces - 954D Fight Against Traffic

Codeforces 954 E. Water Taps

Codeforces 954 D Fight Against Traffic

Codeforces 954E Water Taps

CodeForces954DFight Against Traffic(最短路)