zoukankan      html  css  js  c++  java
  • [Codeforces 460C] Present

    [题目链接]

             https://codeforces.com/contest/460/problem/C

    [算法]

            二分 + 贪心

            要求最小值最大 , 我们不妨二分最小值 , 若一盆花的高度小于二分的值 , 则将这盆花起的w盆花的高度都加一 , 具体实现时可以使用前缀和 + 差分

            时间复杂度 : O(NlogV)

    [代码]

              

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5 + 10;
    const int inf = 2e9;
    
    int n , m , w;
    int a[MAXN];
    
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    inline bool check(int mid)
    {
            int cnt = 0;
            static int delta[MAXN];
            for (int i = 1; i <= n; i++) delta[i] = 0;
            for (int i = 1; i <= n; i++)
            {
                    delta[i] += delta[i - 1];
                    while (a[i] + delta[i] < mid)
                    {
                            if (++cnt > m) return false;
                            delta[i]++;
                            delta[min(i + w,n + 1)]--;
                    }
            }        
            return true;
    }
    
    int main()
    {
            
            read(n); read(m); read(w);
            int l = inf , r = 0;
            for (int i = 1; i <= n; i++) 
            {
                    read(a[i]);
                    l = min(l,a[i]);
                    r = max(r,a[i]);
            }
            r += m;
            int ans = l;
            while (l <= r)
            {
                    int mid = (l + r) >> 1;
                    if (check(mid))
                    {
                            ans = mid;
                            l = mid + 1;
                    } else r = mid - 1;
            }
            printf("%d
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    更好的抽屉效果(ios)
    系统拍照动画
    UITabBarController详解
    touch事件分发
    iOS UWebView详解
    iOS 监听声音按键
    webservice偶尔报黄页,解决方案
    FastReport脚本把数据绑定到文本控件上
    [转]js版的md5()
    JQuery中$.ajax()方法参数详解
  • 原文地址:https://www.cnblogs.com/evenbao/p/9715360.html
Copyright © 2011-2022 走看看