题号:P1040
很有意义,想了不短时间,和一位群里的同学讨论了一下:代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<queue> #include<functional> using namespace std; int n; struct re{ int i; string s; re(){ } re(int i,string s){ this->i=i; this->s=s; } re operator = (const re r){ this->i=r.i; this->s=r.s; return *this; } }f[32][32]; int scores[35]; string construction(int num){ string s=""; while(num!=0){ s.insert(0,1,'0'+num%10); num/=10; } s+=' '; return s; } re dfs(int l,int r){ if(l==r)return re(scores[l],construction(l)); if(l>r)return re(1,""); if(f[l][r].i!=0)return f[l][r]; int mm=0; string s; for(int i=l;i<=r;i++){ re re1=dfs(l,i-1); re re2=dfs(i+1,r); if(mm<re1.i*re2.i+scores[i]){ mm=re1.i*re2.i+scores[i]; s=construction(i)+re1.s+re2.s; } } return f[l][r]=re(mm,s); } int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>scores[i]; re re1=dfs(1,n); cout<<re1.i<<endl; cout<<re1.s; } |