First, beginning with the first AC codes (DP Gangster Act of original links )
#include<bits/stdc++.h> using namespace std; int n,m,f1[110][110][110],f2[110][110][110],a[110],sum[110]; int mod(int x){ return (x%10+10)%10; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){cin>>a[i];sum[i]=sum[i-1]+A [I];} for ( int I = . 1 ; I <= n-; I ++) {A [I + n-] = A [I], SUM [I + n-] = SUM [I] + SUM [n-]; } Memset (F2, 127 , the sizeof (F2)); // F2 record is initialized to a minimum value so that a maximum value for ( int I = . 1 ; I <= 2 * n-; I ++ ) { for ( int J = I; J <= 2 * n-; J ++ ) { [I] F1 [J] [ . 1 ] F2 = [I] [J] [ . 1 ] = MOD (SUM [J] -sum [I- . 1 ]); / / pretreatment each segment value } } for ( intL = . 1 ; L <= n-; L ++) { // interval length for ( int I = . 1 , J = I + L- . 1 ; J <= 2 * n-; I ++, J ++) { // left endpoint for ( int H = 2 ; H <= m; H ++) { // the number of segments for ( int K = I + H- . 1 ; K <J; K ++) { // short point f1 [i] [j] [ h] = max ( F1 [I] [J] [H], F1 [I] [K] [H- . 1 ] * MOD (SUM [J] - SUM [K])); F2 [I] [J] [H] = min (F2 [I] [J], F2 [I] [H] [K] [H- . 1 ] * MOD (SUM [J] - SUM [K])); } } } } int maxn=0,minn=0x7fffffff; for(int i=1;i<=n;i++){//找答案啦~~~ maxn=max(maxn,f1[i][i+n-1][m]); minn=min(minn,f2[i][i+n-1][m]); } cout<<minn<<endl<<maxn; }
Gangster ideas
Set state
DP routine is entitled to ask what is what is located, then there is a range of DP this classic title
So we let F [i] [j] [h] h represent the maximum and minimum divided segments from i to j
Transfer equation
We can draw from the state transition equation:
f[i][j][h]=max/min(f[i][j][h],f[i][k][h-1]*sum)f[i][j][h]=max/min(f[i][j][h],f[i][k][h−1]∗sum)
Enumeration first interval length
DP from the normal range of templates we know
why:
If we first enumerate i, j
For example, i = 1, j = 10, k = 5 when
f [1] [5] [h] it has not been so processed out wrong
And then enumerate about endpoint
This is easy to understand, interval DP Well
Re-enumeration is the number of segments
This is also a good thing to understand the answer to be updated