zoukankan      html  css  js  c++  java
  • [CodeForces954G]Castle 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,&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
    ",Ans);
        return 0;
    }
    
  • 相关阅读:
    Go panic+defer+recover理解加使用
    golang atomic包的使用
    Mongo 锁的理解
    AudioManager(录音)
    SAXpraseHelper
    Density
    CustomDialog
    CustomScrollVeiw(双向滑动)
    tf的concat问题
    tensorflow的gpu版本错误
  • 原文地址:https://www.cnblogs.com/void-f/p/8630129.html
Copyright © 2011-2022 走看看