题解
2023-08-25 10:47:10
发布于:广东
13阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;
int m, n, a[N], s[N], v[N][N];
int dpx[N][N][10], dpn[N][N][10];
int Mod10(int a)
{
if(a >= 0)
return a % 10;
else
return a % 10 + 10;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
a[i+n] = a[i];
}
for(int i = 1; i < 2*n; ++i)
s[i] = s[i-1] + a[i];
for(int l = 1; l <= n; ++l)
for(int i = 1; i+l-1 < 2*n; ++i)
{
int j = i+l-1;
v[i][j] = Mod10(s[j]-s[i-1]);
}
memset(dpn, 0x3f, sizeof(dpn));
for(int l = 1; l <= n; ++l)
for(int i = 1; i <= n; ++i)
{
int j = i+l-1;
dpx[i][j][1] = dpn[i][j][1] = v[i][j];
for(int k = 2; k <= m && k <= l; ++k)
for(int h = i+k-1; h <= j; ++h)
{
dpx[i][j][k] = max(dpx[i][j][k], dpx[i][h-1][k-1]*v[h][j]);
dpn[i][j][k] = min(dpn[i][j][k], dpn[i][h-1][k-1]*v[h][j]);
}
}
int mx = 0, mn = INF;
for(int i = 1; i <= n; ++i)
{
int j = i+n-1;
mx = max(mx, dpx[i][j][m]);
mn = min(mn, dpn[i][j][m]);
}
cout << mn << endl << mx;
return 0;
}
9ms + AC代码
这里空空如也
有帮助,赞一个