最大值最小化

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
****************************************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值