NC51171. Polygon
描述
输入描述
Your program is to read from standard input. The input describes a polygon with N vertices. It contains two lines. On the first line is the number N. The second line contains the labels of edges 1, ..., N, interleaved with the vertices' labels (first that of the vertex between edges 1 and 2, then that of the vertex between edges 2 and 3, and so on, until that of the vertex between edges N and 1), all separated by one space. An edge label is either the letter t (representing +) or the letter x (representing *).
For any sequence of moves, vertex labels are in the range [-32768,32767].
输出描述
Your program is to write to standard output. On the first line your program must write the highest score one can get for the input polygon. On the second line it must write the list of all edges that, if removed on the first move, can lead to a game with that score. Edges must be written in increasing order, separated by one space.
示例1
输入:
4 t -7 t 4 x 2 x 5
输出:
33 1 2
C++ 解法, 执行用时: 4ms, 内存消耗: 520K, 提交时间: 2022-01-11 14:22:02
#include<bits/stdc++.h> using namespace std; int n,ans=-INT_MAX,a[101],f[200][200],g[200][200]; char c[101]; int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>c[i]>>a[i],getchar(),a[n+i]=a[i],c[n+i]=c[i]; for(int i=1;i<=(n<<1);i++)for(int j=1;j<=(n<<1);j++)f[i][j]=-INT_MAX,g[i][j]=INT_MAX; for(int i=1;i<=(n<<1);i++)f[i][i]=g[i][i]=a[i]; for(int len=2;len<=n;len++)for(int i=1,j=len;j<=(n<<1);i++,j++)for(int k=i;k<j;k++){ if(c[k+1]=='x')f[i][j]=max(f[i][j],max(f[i][k]*f[k+1][j],max(g[i][k]*g[k+1][j],max(f[i][k]*g[k+1][j],g[i][k]*f[k+1][j])))),g[i][j]=min(g[i][j],min(f[i][k]*f[k+1][j],min(g[i][k]*g[k+1][j],min(f[i][k]*g[k+1][j],g[i][k]*f[k+1][j])))); else if(c[k+1]=='t')f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]),g[i][j]=min(g[i][j],g[i][k]+g[k+1][j]); } for(int i=1;i<=n;i++)ans=max(ans,f[i][i+n-1]); cout<<ans<<"\n"; for(int i=1;i<=n;i++)if(f[i][i+n-1]==ans)cout<<i<<" "; return 0; }