最小m段和问题(划分dp)

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 <iostream>
#include <algorithm>

using namespace std;

int n, m;
int dp[1005][1005];
int sum[1005][1005];
int a[1005];

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            dp[i][j] = 1000000000;
        }
    }
    for(int i = 1; i <= n; i++) {
        int num = 0;
        for(int j = i; j <= n; j++) {
            num += a[j];
            sum[i][j] = num;
        }
        dp[i][1] = sum[1][i];
    }
    for(int i = 2; i <= n; i++) {
        for(int j = 2; j <= i && j <= m; j++) {
            int p;
            for(int k = 1; k < i; k++) {
                dp[i][j] = min(dp[i][j], max(dp[k][j - 1], sum[k + 1][i]));
            }
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38367681/article/details/81252798