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;
    }
    

      

  • 相关阅读:
    jQuery横向手风琴
    jQuery宽屏游戏焦点图
    手风琴式相册图片展开效果
    鼠标悬停图片分享按钮动画
    jQuery水平滑动菜单
    jQuery图片水平滑动延迟加载动画
    jQuery悬浮焦点图宽屏
    jQuery自定义美化下拉框
    纯CSS3垂直动画菜单
    面向服务与微服务架构
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4675880.html
Copyright © 2011-2022 走看看