Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5472 | Accepted: 2334 |
Description
Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N.
On the first move, one of the edges is removed. Subsequent moves involve the following steps:
�pick an edge E and the two vertices V1 and V2 that are linked by E; and
�replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2.
The game ends when there are no more edges, and its score is the label of the single vertex remaining.
Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.
Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.
On the first move, one of the edges is removed. Subsequent moves involve the following steps:
�pick an edge E and the two vertices V1 and V2 that are linked by E; and
�replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2.
The game ends when there are no more edges, and its score is the label of the single vertex remaining.
Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.
Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.
Input
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 *).
3 <= N <= 50
For any sequence of moves, vertex labels are in the range [-32768,32767].
3 <= N <= 50
For any sequence of moves, vertex labels are in the range [-32768,32767].
Output
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.
Sample Input
4 t -7 t 4 x 2 x 5
Sample Output
33 1 2
Source
大概就是一个比较简单的dp,枚举中间断开的位置,唯一注意的就是要维护两个值最大值和最小值,因为存在一种情况最小值*最小值反而负负得正了。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 const int N = 200 + 13 ; 5 using namespace std ; 6 int n,a[N]; 7 char ss[N]; 8 long long f[N][N],g[N][N],ans; 9 10 void Init() 11 { 12 scanf("%d",&n); 13 for(int i = 1 ; i <= n ; ++i) 14 { 15 cin>>ss[i]>>a[i]; 16 a[n+i]=a[i],ss[n+i]=ss[i]; 17 } 18 } 19 20 21 void Solve( ) 22 { 23 for(int i = 1 ; i <= (n<<1) ; ++i) for(int j = 1 ; j <= (n<<1) ; ++j)f[i][j] = -0x3f3f3f3f ; 24 for(int i = 1 ; i <= (n<<1) ; ++i) for(int j = 1 ; j <= (n<<1) ; ++j)g[i][j] = 0x3f3f3f3f ; 25 for(int i = 1 ; i <= (n<<1) ; ++i) 26 { 27 if(ss[i+1]=='t') 28 f[i][i+1] = g[i][i+1] = a[i] + a[i+1]; 29 else 30 f[i][i+1] = g[i][i+1] = a[i] * a[i+1]; 31 f[i][i] = g[i][i] = a[i]; 32 } 33 long long ans = -0x3f3f3f3f ; 34 for(int i = (n<<1);--i;) 35 for(int j = i + 2 ; j <=(n<<1);++j) 36 for(int k = i;k<j;++k) 37 if(ss[k+1] == 't') 38 { 39 f[i][j] = max(f[i][j],f[i][k]+f[k+1][j]); 40 g[i][j] = min(g[i][j],g[i][k]+g[k+1][j]); 41 } 42 else 43 { 44 long long int a = f[i][k]*g[k+1][j],b = f[i][k]*f[k+1][j]; 45 long long int c = g[i][k]*f[k+1][j],d = g[i][k]*g[k+1][j]; 46 f[i][j] = max(f[i][j],max(max(a,b),max(c,d))); 47 g[i][j] = min(g[i][j],min(min(a,b),min(c,d))); 48 } 49 for(int i = 1; i<= n ;++i) 50 ans = max(ans,f[i][i+n-1]); 51 printf("%lld ",ans); 52 for(int i=1;i<=n;++i) 53 if(f[i][i+n-1] == ans) 54 printf("%d ",i); 55 puts(""); 56 } 57 58 59 60 int main( ) 61 { 62 // freopen("polygon.in","r",stdin); 63 // freopen("polygon.out","w",stdout); 64 Init(); 65 Solve(); 66 fclose(stdin); 67 fclose(stdout); 68 return 0 ; 69 }