题解
计算分数是搜索
存储前缀注意细节
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll sco[35][35]={0};
string pre[35][35];
ll a[35]={0};
queue<ll> q;
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
string turn(ll x)
{
string s;
while(x)
{
char d='0'+x%10;
s=d+s;
x/=10;
}
return s;
}
ll ss(ll l,ll r)
{
if(r<l)
{
sco[l][r]=1;
pre[l][r]="";
return 1;
}
if(l==r)
{
sco[l][r]=a[l];
pre[l][r]=turn(l)+' ';//细节
return a[l];
}
if(sco[l][r]) return sco[l][r];
ll val=0,index;
for(ll i=l;i<=r;i++)
{
ll next=ss(l,i-1)*ss(i+1,r)+a[i];
if(next>val)
{
val=next;
index=i;
string add=turn(i);
pre[l][r]=add+' '+pre[l][i-1]+pre[i+1][r];//细节每个数字后跟一个空格
}
}
sco[l][r]=val;
return sco[l][r];
}
int main()
{
ll n;
read(n);
for(ll i=1;i<=n;i++) read(a[i]);
ll index,val=0;
write(ss(1,n));
putchar('\n');
cout<<pre[1][n]<<endl;
return 0;
}
标签:pre,10,sco,string,NOIP2003,ll,35,P1040,二叉树
From: https://www.cnblogs.com/pure4knowledge/p/18050289