HDU 1244 Max Sum Plus Plus Plus - dp

传送门

题目大意:

给一个序列,要求将序列分成m段,从左至右每一段分别长l1,l2,...lm,求最大的和是多少。

题目分析:

和最大m段子段和相似,先枚举\(i \in [1,m]\),然后$j \in [num[m], n] $,dp转移为: \[dp[j][i] = max(dp[j - 1][i], dp[j - num[i]][i - 1] + sum[j] - sum[i - num[i]])\]

code:

#include
#include
#include
#include
#include
#include
#include
//#include
using namespace std;

const int N = 1005, M = 25;
int n, m, num[N];
typedef long long ll;
ll sum[N], f[N][N];

inline int read(){
    ll i = 0, f = 1; char ch = getchar();
    for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
    if(ch == '-') f = -1, ch = getchar();
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        i = (i << 3) + (i << 1) + (ch - '0');
    return i * f;
}

inline void wr(ll x){
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) wr(x / 10);
    putchar(x % 10 + '0');
}

int main(){
    while(n = read()){
        m = read();
        sum[0] = 0;
        for(int i = 1; i <= m; i++) num[i] = read();
        for(int i = 1; i <= n; i++){
            ll x = read() * 1LL;
            sum[i] = sum[i - 1] + x;
        }
        int now = 0;
        for(int i = 1; i <= m; i++){
            now += num[i];
            for(int j = now; j <= n; j++)
                f[j][i] = max(f[j - 1][i], f[j - num[i]][i - 1] + sum[j] - sum[j - num[i]]);
        }
        wr(f[n][m]), putchar('\n');
    }
}

转载于:https://www.cnblogs.com/CzYoL/p/7659206.html

你可能感兴趣的:(HDU 1244 Max Sum Plus Plus Plus - dp)