题目:Snakes
题目链接:https://codeforces.ml/group/5yyKg9gx7m/contest/269908/problem/B
题目描述:
给出n群🐍,按顺序捕蛇。🐍网初始大小为最大的🐍群。有k次调整🐍网大小的机会。求最小浪费。
题目分析:
先求出前几个蛇群除始浪费值。
在根据次数求前几个最小浪费值。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[405],s[405],dp[405][405];
int main()
{
int n,k,i,j,p,ma;
memset(dp,0x3f,sizeof(dp)); //无穷化
scanf("%d%d",&n,&k);
for(i=1,ma=0;i<=n;i++)
{
scanf("%d",&a[i]); //求前缀和
s[i]=a[i]+s[i-1];
ma=max(ma,a[i]);
dp[i][0]=ma*i-s[i];
}
for(i=1;i<=k;i++)
{
for(j=i+1;j<=n;j++)
{
ma=a[j];
for(p=j;p>i;p--)
{
ma=max(ma,a[p]);
dp[j][i]=min(dp[j][i],dp[p-1][i-1]+ma*(j-p+1)-s[j]+s[p-1]); //根据分组更新
}
}
}
printf("%d",dp[n][k]);
return 0;
}