代码如下:注释部分为思路讲解
///hnu Cent Saving
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 2000, D = 20;
const int infty = 0xfffffff;
int Prize[N];
int Cost[N+1][D+1];
int rnd (int p)
{ return 10*((p+5)/10);
}
int main()
{
int n, d, i, j, k, s;
scanf("%d %d\n", &n, &d);
for (i = 0; i < n; i++)
scanf("%d", &Prize[i]);
for(i = 0; i <= n; i++)
{
for(j = 0; j <= d; j++)
{
if(i==0)
{
Cost[i][j] = 0;///当没有物品时无论放多少块隔板都为0
}
else if (j == 0)///当没有隔板时,价值就相当于从0到i的总价值再舍入
{
s = 0;
for(k = 0; k < i; k++)
s += Prize[k];
Cost[i][j] = rnd(s);
}
else ///当有隔板时
{
s = 0;
Cost[i][j] = infty;
for (k = i-1; k >= 0; k--)///枚举在第i个物品之前的i个空中放隔板所能得到的最小值
{
s += Prize[k];///min(前i个物品放j个隔板所能得到的最小值,在第k个物品前放j-1个隔板所得最小值+k到i的舍入)
Cost[i][j] = min(Cost[i][j], rnd(s) + Cost[k][j-1]);
}
}
}
}
printf("%d\n", Cost[n][d]);
return 0;
}