原文链接:https://ac.nowcoder.com/acm/problem/16675
AC代码:
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int n,m;
int mapp[105],dp[105][105][15],sum[105],ans0=0,ans1=1e8,dp1[105][105][15];
int mod(int x){
return (x%10+10)%10;
}
int main(){
int i,j,k;
memset(mapp,0,sizeof(mapp));
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
cin>>n>>m;
for(i=1;i<=n;i++){
cin>>mapp[i];
sum[i]+=sum[i-1]+mapp[i];
//cout<<i<<" "<<sum[i]<<endl;
}
for(i=n+1;i<=2*n;i++){
mapp[i]=mapp[i-n];
sum[i]+=sum[i-1]+mapp[i];
//cout<<i<<" "<<sum[i]<<endl;
}
for(i=0;i<105;i++){
for(j=0;j<105;j++){
for(k=0;k<15;k++){
dp1[i][j][k]=1e8;
}
}
}
for(int len=0;len<n;len++){
for(i=1;i+len<=2*n;i++){
j=i+len;
dp[i][j][1]=mod(sum[j]-sum[i-1]);
dp1[i][j][1]=mod(sum[j]-sum[i-1]);
}
}
for(k=2;k<=m;k++){
for(int len=1;len<n;len++){
for(i=1;i+len<=2*n;i++){
j=i+len;
for(int l=i+k-2;l<j;l++){
dp[i][j][k]=max(dp[i][j][k],dp[i][l][k-1]*mod(sum[j]-sum[l]));
//cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
dp1[i][j][k]=min(dp1[i][j][k],dp1[i][l][k-1]*mod(sum[j]-sum[l]));
}
}
}
}
for(i=1;i<=n;i++){
ans0=max(ans0,dp[i][i+n-1][m]);
ans1=min(ans1,dp1[i][i+n-1][m]);
}
cout<<ans1<<endl<<ans0<<endl;
return 0;
}