题解

1 条题解

  • 0
    @ 2017-08-20 21:36:21

    --------------------------------------------AC code--------------------------------------------

    #include<cstdio>
    
    using namespace std;
    
    const int MAXN = 50005;
    int n, m, t[MAXN<<1], ans, l, r;
    
    inline int max(int a, int b){
        return a > b ? a : b;
    }
    
    int main(){
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) scanf("%d", &t[i]), t[i+n] = t[i], r += t[i], l = max(l, t[i]);
        if(m >= n){
            printf("%d", l);
            return 0;
        }
        if(m == 1){
            printf("%d", r);
            return 0;
        }
        while(l < r){
            int mid = l + (r-l)/2, num, all = 0;
            for(int i = 1; i <= n; i++){
                all += t[i];
                if(all > mid)   break;
                int sum = 0;
                num = 1;
                for(int j = i; j <= n+i-1; j++){
                    sum += t[j];
                    if(sum > mid){
                        num++;
                        sum = t[j];
                    }
                }
                if(num <= m)    break;
            }
            if(num > m) l = mid+1;
            else    r = mid;
        }
        printf("%d", l);
        return 0;
    }
    
  • 1

信息

难度
(无)
分类
二分查找其他 点击显示
标签
递交数
0
已通过
0
通过率
?
上传者