Problem Description
给你一个长为n(10<=n<=10000)的数组,数组中的每一个数大于等于1小于等于1000000。请你找出一个长为k(100<=k<=1000)的子序列。找序列时,假如第一个数找的是数组中的第i个位置的数,那么找第二个数时只能找数组中第i个位置后的数,依次找出k个数。使得第一个数*1+第二个数*2+…+第k个数*k的值最小。
Input
有多组(小于11组)测试数据,每组第一行输入n和k(用空格隔开),第二行输入n个数(数之间用空格隔开)。
Output
请输出最小的和。
Sample Input
15 5
5 4 3 2 1 1 2 3 4 5 5 4 3 2 1
Sample Output
19
思路:
最近有种感觉就是一看见这种什么什么序列的就感觉要用动态规划写动态转移方程,还是太年轻了。这个题的方程为:
dp[1][j]=dp[0][j-1]+a[j]*j
其中dp[0][j-1]保存的是1~j-1中的数作为第i-1个的最小值
#include <iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAX = 10200;
const int INF = 0x3f3f3f3f;
int dp[2][MAX];
int p[MAX];
int main()
{
int m, n;
while (cin >> m >> n) {
memset(p, 0, sizeof(p));
for (int i = 1; i <= m; i++)
cin >> p[i];
int ans;
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
ans = INF;
for (int j = i; j <= m; j++) {
dp[1][j] = dp[0][j - 1] + p[j] * (i);
dp[0][j - 1] = ans;
ans = min(dp[1][j], ans);
}
}
cout << ans << endl;
}
//system("pause");
return 0;
}
/***************************************************
User name:
Result: Accepted
Take time: 96ms
Take Memory: 340KB
Submit time:
****************************************************/