zoukankan      html  css  js  c++  java
  • Codeforces 954 G Castle Defense

    Discription

    Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections jthat |i - j| ≤ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.

    Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.

    There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.

    Input

    The first line of the input contains three integers nr and k (1 ≤ n ≤ 500 000, 0 ≤ r ≤ n0 ≤ k ≤ 1018) — the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the current number of archers at each section.

    Output

    Print one integer — the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.

    Example

    Input
    5 0 6
    5 4 3 4 9
    Output
    5
    Input
    4 2 0
    1 2 3 4
    Output
    6
    Input
    5 1 1
    2 1 2 1 2
    Output
    3


    NOIP水平的二分答案,,二分了一个值之后直接贪心

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=1000005;
    ll a[maxn],b[maxn],k,now;
    ll n,l=1<<30,r,mid,ans,R,alr;
    
    inline bool calc(){
    	memcpy(b,a,sizeof(a)),now=0,alr=0;
    	for(int i=0;i<=R;i++) now+=b[i+1];
    	for(int i=1;i<=n;i++,now+=b[i+R]){
    		if(now<mid){
    			alr+=mid-now;
    			b[i+R]+=mid-now;
    			now=mid;
    			if(alr>k) return 0;
    		}
    		if(i>R) now-=b[i-R];
    	}
    	return 1;
    }
    
    int main(){
    	scanf("%I64d%I64d%I64d",&n,&R,&k);
    	for(int i=1;i<=n;i++){
    		scanf("%I64d",a+i);
    		l=min(l,a[i]);
    	}
    	r=2ll*1e18;
    	while(l<=r){
    		mid=l+r>>1;
    		if(calc()) ans=mid,l=mid+1;
    		else r=mid-1;
    	}
    	printf("%I64d
    ",ans);
    	return 0;
    }
    

      

     
  • 相关阅读:
    【2020-04-14】吃一折,长一智吧
    对“沟通成本”模型的一个重新假设
    【2020-04-13】稀缺才能让人珍惜
    【2020-04-12】决策都是当前认知的反映
    hhhhh我进步啦!
    求后序遍历(信息学奥赛一本通 1339)
    数的划分(信息学奥赛一本通 1304 洛谷 1025)
    memset函数怎么用嘞↓↓↓
    stack函数怎么用嘞?↓↓↓
    终于开通博客啦!
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8672758.html
Copyright © 2011-2022 走看看