Codeforces 460C Present【二分+思维优化】

C. Present
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.

There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?

Input

The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the maximum final height of the smallest flower.

Examples
Input
6 2 3
2 2 2 2 1 1
Output
2
Input
2 5 1
5 8
Output
9
Note

In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.


题目大意:


一共有N盆花,每一盆花的初始高度已知,我们一共可以对连续的W朵花进行浇水m次,每次对一盆花浇水可以让其生长1高度,问最优分配情况下,最小高度的花的最高高度为多少。


思路:


1、很显然,问题的关键点在于两个,一个是m次,另外一个是最小高度。如果我们直接对M次进行处理,时间复杂度显然是不科学的,那么考虑从这个最小高度出发考虑问题。


2、对于这个最小最高高度,我们可以通过直接枚举然后贪心处理,判断此时这种最小高度的情况最少需要多少天,如果天数小于m,那么很显然这个高度是可以的,继续枚举,直到这个当前枚举到的高度是不可能的为止,考虑到这种单调性,我们考虑二分这个最小最高高度。


3、对于当前枚举值mid.我们贪心的处理,每当遇到a【i】<mid的时候,这个花盆是一定需要进行mid-a【i】天的浇花处理的,对应其后边连续的W个花盆,随之也要有连续mid-a【i】天的浇花处理。但是这里暴力贪心的时间复杂度是O(NW),也是很高的一个数量级,那么考虑思维优化:

①对应当前假设是第i盆花,那么显然,对应i+1,i+2,i+3............i+w-1这些盆花是都需要进行处理的。

②那么我们只要保证在i+1,i+2,i+3,i+4..........i+w-1这些花盆上累加浇花次数即可。那么定义一个sum,表示当前第i盆花在判断之前,被浇了多少天了。那么我们动态处理sum即可。时间复杂度O(n);

③这一块的代码实现不难,嘴有点笨,大家不如直接看Ac代码~


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll a[1000000];
ll b[1000000];
ll vis[100060];
ll n,m,w;
int Slove(ll mid)
{
    ll cont=0;
    ll sum=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        sum+=vis[i];
        if(cont>m)return 0;
        if(a[i]+sum<mid)
        {
            cont+=mid-(a[i]+sum);
            ll tmp=mid-(a[i]+sum);
            sum+=tmp;
            vis[i+w]=-tmp;
        }
    }
    if(cont>m)return 0;
    else return 1;
}
int main()
{
    while(~scanf("%I64d%I64d%I64d",&n,&m,&w))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
        }
        ll ans=0;
        ll l=1;
        ll r=2000000000;
        while(r-l>=0)
        {
            ll mid=(l+r)/2;
            int t=Slove(mid);
            if(t==1)
            {
                l=mid+1;
                ans=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        printf("%I64d\n",ans);
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值