Luo Valley P1040 plus binary & [NOIP2003 improve group] (interval dp, the tree traversal)

Portal


Problem-solving ideas

For the entire preorder, do it again the interval dp:

Enumerate all nodes in the interval [l ... r] as the root node, and then calculate the score, if larger than the current value while updating the value dp and root [l] [r] (in order to output first traversal) .

Preorder around the root, the output of the recursive like.

AC Code

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int n,a[35],dp[35][35],root[35][35];
 6 void print(int l,int r){ 
 7     if(l>r) return;
 8     if(l==r) printf("%d ",l);
 9     else{
10         printf("%d ",root[l][r]);
11         print(l,root[l][r]-1);
12         print(root[l][r]+1,r);
13     }
14 }
15 int main()
16 {
17     cin>>n;
18     for(int i=1;i<=n;i++) cin>>a[i];
19     for(int i=1;i<=n;i++) dp[i][i]=a[i];
20     for(int i=1;i<=n;i++){
21         for(int j=0;j<i;j++) dp[i][j]=1;
22     }
23     for(int len=2;len<=n;len++){
24         for(int i=1;i<=n;i++){
25             int j=i+len-1;
26             for(int k=i;k<=j;k++){
27                 if(dp[i][j]<dp[i][k-1]*dp[k+1][j]+a[k]){
28                     dp[i][j]=dp[i][k-1]*dp[k+1][j]+a[k];
29                     root[i][j]=k;
30                 }
31             }
32         }
33     }
34     cout<<dp[1][n]<<endl;
35     print(1,n);
36     return 0;
37 }

// NOIP2003 improve group t3

Guess you like

Origin www.cnblogs.com/yinyuqin/p/12358357.html