注意要遍历完整个2倍区间!!!
调了好久!
#include<iostream> #include<cstdio> #define ri register int #define u int namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } return x*f; } } using opt::in; #define NN 105 #include<algorithm> namespace mainstay { u N,fh[NN<<1],f[NN<<1][NN<<1][2]; inline void solve() { while(~scanf("%d",&N)) { for(ri i(1); i<=N; ++i) { char _s; u _x; scanf(" %c %d",&_s,&_x); fh[i]=fh[i+N]=(_s=='t'); f[i][i][1]=f[i][i][0]=f[i+N][i+N][1]=f[i+N][i+N][0]=_x; } for(ri len(2); len<=N; ++len) { for(ri i(1),j(i+len-1); i<=(N<<1); ++i,++j) { if(j>(N<<1)) break; f[i][j][1]=-0x3f3f3f3f,f[i][j][0]=0x3f3f3f3f; for(ri k(i); k<j; ++k) { if(fh[k+1]) { f[i][j][1]=std::max(f[i][j][1],f[i][k][1]+f[k+1][j][1]); f[i][j][0]=std::min(f[i][j][0],f[i][k][0]+f[k+1][j][0]); } else { f[i][j][1]=std::max(f[i][j][1],f[i][k][1]*f[k+1][j][1]); f[i][j][1]=std::max(f[i][j][1],f[i][k][0]*f[k+1][j][0]); f[i][j][1]=std::max(f[i][j][1],f[i][k][0]*f[k+1][j][1]); f[i][j][1]=std::max(f[i][j][1],f[i][k][1]*f[k+1][j][0]); f[i][j][0]=std::min(f[i][j][0],f[i][k][1]*f[k+1][j][1]); f[i][j][0]=std::min(f[i][j][0],f[i][k][0]*f[k+1][j][0]); f[i][j][0]=std::min(f[i][j][0],f[i][k][0]*f[k+1][j][1]); f[i][j][0]=std::min(f[i][j][0],f[i][k][1]*f[k+1][j][0]); } } } } u mx(-0x3f3f3f3f); for(ri i(1); i<=N; ++i) { if(f[i][i+N-1][1]>mx) { mx=f[i][i+N-1][1]; } } printf("%d ",mx); for(ri i(1); i<=N; ++i) { if(f[i][i+N-1][1]==mx) { printf("%d ",i); } } printf(" "); } } } int main() { //freopen("x.txt","r",stdin); std::ios::sync_with_stdio(false); mainstay::solve(); }