Portal
Problem-solving ideas
Interval dp: General dp [i] [j] representing the interval i ... j answer, the answer can be transferred from one room or from a certain area.
Most problem solution is to make the outer loop enumeration len, representing the interval length, and the left end of the interval i enumerate, then calculate the right point j, and then enumerate all breakpoints i to j in accordance with DP [i ] [k], dp [k + 1] [j] calculated dp [i] [j].
This is a problem because one week is a ring, so we were off to the ring for the operating chain.
AC Code
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=205; 6 int n,a[maxn],dp1[maxn][maxn],dp2[maxn][maxn],s[maxn]; 7 int main() 8 { 9 cin>>n; 10 for(int i=1;i<=n;i++){ 11 cin>>a[i]; 12 } 13 for(int i=1;i<=n-1;i++){ 14 a[n+i]=a[i]; 15 } 16 for(int i=1;i<=2*n-1;i++){ 17 s[i]=s[i-1]+a[i]; 18 } 19 for(int len=2;len<=n;len++){ 20 for(int i=1;i<=2*n-1;i++){ 21 int j=i+len-1; 22 if(j>2*n-1) break; 23 dp2[i][j]=0x3f3f3f3f; 24 for(int k=i;k<j;k++){ 25 dp1[i][j]=max(dp1[i][j],dp1[i][k]+dp1[k+1][j]); 26 dp2[i][j]=min(dp2[i][j],dp2[i][k]+dp2[k+1][j]); 27 } 28 dp1[i][j]+=s[j]-s[i-1]; 29 dp2[i][j]+=s[j]-s[i-1]; 30 } 31 } 32 int ansmax=0,ansmin=0x3f3f3f3f; 33 for(int i=1;i<=n;i++){ 34 ansmax=max(ansmax,dp1[i][i+n-1]); 35 ansmin=min(ansmin,dp2[i][i+n-1]); 36 } 37 cout<<ansmin<<endl<<ansmax<<endl; 38 return 0; 39 }
//NOI1995 Day?T2