zoukankan      html  css  js  c++  java
  • CodeForces 460C——二分+前缀和—— Present

    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 nm 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.

    Sample Input

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

    Hint

    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.

    /*
    二分枚举最小高度
    用前缀pre表示从i开始到n浇水的次数,因为宽度为w所以后面都要减去现在所浇的次数
    如果到最后总浇水次数大于m说明高度还要更高,对于右区间进行二分
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int inf = 1e9 + 1e6;
    const int MAX = 1e6 + 10;
    int pre[MAX], a[MAX];
    int n, m, w, sum;
    bool judge(int x)
    {
        int sum = 0;
        memset(pre, 0, sizeof(pre));
        for(int i = 1; i <= n; i++){
            pre[i] += pre[i-1];
            int   day = x - (a[i] + pre[i]);
            if(day > 0){
                sum += day;
                pre[i] += day;
                pre[i+w] -= day;
            }
            if(sum > m) return false;
        }
        return true;
    }
    
    int main()
    {
        while(~scanf("%d%d%d", &n, &m, &w)){
            for(int i = 1; i <= n ; i++){
                scanf("%d", &a[i]);
            }
            int l = 1, r = inf;
            int ans = l;
            while(l <= r){
                int mid = (l + r) >> 1;
                if(judge(mid)){
                    l = mid + 1;
                    ans = mid;
                }
                else r = mid - 1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    第4次作业(条件)比较大小。第3次作业(条件)计算火车运行时间。
    GitHub搜索技巧
    flex实现左中固定不变,右边自适应
    JavaScript高级__原型继承+组合继承
    JavaScript高级__深入了解闭包
    JavaScript高级__执行上下文代码案例
    JavaScript中的显式原型链 prototype 和隐式原型 __proto__
    谷歌强大插件收集,持续更新。。。
    js中~~和^=
    vue自定义指令----directive
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4675880.html
Copyright © 2011-2022 走看看