Codeforces 954 G. Castle Defense

http://codeforces.com/problemset/problem/954/G

 

二分答案

检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越好

差分即可

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 500001

typedef long long LL;

int n,r; 
LL k;

LL sum[N];
LL cf[N];

template<typename T>
void read(T &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}

bool check(LL x)
{
    LL rest=k,need;
    LL add=0;
    memset(cf,0,sizeof(cf));
    for(int i=1;i<=n;++i)
    {
        add+=cf[i];
        if(sum[i]+add<x) 
        {
            need=x-sum[i]-add;
            rest-=need;
            if(rest<0) return false;
            cf[i+1]+=need;
            if(i+2*r<n) cf[i+2*r+1]-=need;
        }
    }
    return true;
}

int main()
{
    read(n); read(r); read(k);
    int x;
    for(int i=1;i<=n;++i)
    {
        read(x);
        sum[max(0,i-r)]+=x;
        if(i+r<n) sum[i+r+1]-=x;
    }
    for(int i=1;i<=n;++i) sum[i]+=sum[i-1];
    LL l=0,r=2e18,mid,ans=0;
    while(l<=r)
    {
        mid=l+r>>1;
        if(check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    cout<<ans;
}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8625233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值