codevs 3278 最小m 段和问题

时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 黄金 Gold
题目描述  Description

给定 n 个整数(不一定是正整数)组成的序列,现在要求将序列分割为 m 段,每段子序列中的数在原序列 中连续排列。如何分割才能使这 m 段子序列的和的最大值达到最小?

输入描述  Input Description

 文件的第 1 行中有 2 个正整数 n 和 m。 正整数 n 是序列 的长度;正整数 m 是分割的断数。 接下来的一行中有 n 个整数。

输出描述  Output Description

文件的第 1 行中的数是计算出 的 m 段子序列的和的最大值的最小值。

样例输入  Sample Input

1 1

10

样例输出  Sample Output

10

数据范围及提示  Data Size & Hint

N<=1000,M<=N

二分做法 点击传送

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int ans,l,r,n,m,i,a[1000+1];
int judge(int now)
{
    int tot=0,x=0;
    for(i=0;i<n;++i)
    {
        if(a[i]>now) return 0;
        if(a[i]+x<=now)
        x+=a[i];
        else 
        {
            x=a[i];
            tot++;
            if(tot>=m) return 0;
        }
    }
    return 1;
}
int main()
{
    cin>>n>>m;
    for(i=0;i<n;++i)
    {
        cin>>a[i];
        r+=a[i];
    }
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(judge(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<ans;
}

 

转载于:https://www.cnblogs.com/ruojisun/p/6391595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值