【SDUT】[2146]Minimum Subsequence Sum

Problem Description

Problem Analysis

Minimum subsequence sum DP problem

Here, consider selecting the j-th number as the i-th number of the subsequence.

Then dp[1][j]=dp[0][j-1]+a[j]*j

Here dp[0][j-1] stores the minimum value using a number from 1 to j-1 as the (i-1)-th number.

You can understand it with the code below:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<string.h>
int a[10200];
int dp[2][10200];
int main() {
	int n,k;
	while(scanf("%d %d",&n,&k)!=EOF) {
		memset(dp,0,sizeof(dp));
		for(int i=1; i<=n; i++)
			scanf("%d",&a[i]);
		int res;
		for(int i=1; i<=k; i++) {
			res=999999999;
			for(int j=i; j<=n; j++) {
				dp[1][j]=dp[0][j-1]+a[j]*i;
				dp[0][j-1]=res;
				if(res>dp[1][j])
					res=dp[1][j];
			}
		}
		printf("%d\n",res);
	}
	return 0;
}

Loading...