您的位置:首页 > 其它

最大值最小化

2015-05-29 20:14 295 查看


1043: 最大值最小化

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 79 Solved: 14

[Submit][Status][Web
Board]


Description

把一个包含n个正整数的序列划分成m个连续的子序列(每个正整数恰好属于一个序列)。设第i个序列的各数之和为S(i),设S(1),…,S(m)的最大值为MAX,请通过划分使得MAX最小。
例如序列1 2 3 2 5 4划分为3个子序列的最优方案为 1 2 3 | 2 5 | 4,其中S(1),S(2),S(3)分别为6,7,4,那么最大值为7;
如果划分为 1 2 | 3 2 | 5 4,则最大值为9,不是最小。


Input

输入包含多组测试样例。以文件尾为结束。
第一行包含两个数字n(0<n<=100000),m(0<m<=n).n表示序列的数字个数,m表示划分为m个子序列。
第二行包含n个正整数a1,…ai,…an.(0<ai<=10^9)


Output

输出MAX的最小值。每个输出占一行。


Sample Input

6 3
1 2 3 2 5 4
5 2
4 2 1 8 3


Sample Output

7
11


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
long long  c[222222];
int Max, n,m;
long long  sum = 0;
bool is(long long  x)
{
    long long s = 0;
    int cent = 0;
    for(int i = 0 ; i < n ; i++)
    {
 
        if(s + c[i] > x)
        {
            cent++;
            s = c[i];
            if(cent == m)
            {
                return false;
            }
        }
        else s += c[i];
    }
    return true;
}
long long  answer()
{
    long long  x,y;
     x = Max , y = sum;
    while(x < y)
    {
        long long  mid = x + (y-x)/2;
        if(is(mid)) y = mid;
        else x = mid + 1;
    }
    return x;
}
int main()
{
 
    while(scanf("%d%d",&n,&m) != EOF)
    {
        Max = 0;
        sum = 0;
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d",&c[i]);
            if(c[i] > Max) Max = c[i];
            sum += c[i];
        }
        long long  ans = answer();
        printf("%lld\n",ans);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1043
    User: AC_XXZ
    Language: C++
    Result: Accepted
    Time:136 ms
    Memory:3424 kb
****************************************************************/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: