Polygon
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4602 | Accepted: 1921 |
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
感觉这道题和石子合并比较像,只是多了一个乘法。
我刚开始一直WA,后来才想到当操作是乘法时,最大值可能由两边的最大值与最大值、最大值与最小值、最小值与最大值、最小值与最小值相乘得到,所以在DP时必须考虑这四种情况,用一个数组存最大值,一个数组存最小值。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int M=100000; 6 7 int dpmax[60][60],dpmin[60][60]; 8 char c[60]; 9 10 11 int main(){ 12 int n; 13 while(cin >> n){ 14 getchar(); 15 for(int i=0;i<n;i++){ 16 int num; 17 scanf("%c %d",c+i,&num); 18 getchar(); 19 dpmax[i][i]=num; 20 dpmin[i][i]=num; 21 } 22 for(int j=1;j<n;j++){ 23 for(int i=0;i<n;i++){ 24 int a=(i+j)<n?(i+j):((i+j)%n); 25 dpmax[i][a]=-M; 26 dpmin[i][a]=M; 27 for(int k=0;k<j;k++){ 28 if(c[(i+k+1)%n]=='t'){ 29 if(dpmax[i][a]<dpmax[i][(i+k)%n]+dpmax[(i+k+1)%n][a]) 30 dpmax[i][a]=dpmax[i][(i+k)%n]+dpmax[(i+k+1)%n][a]; 31 if(dpmin[i][a]>dpmin[i][(i+k)%n]+dpmin[(i+k+1)%n][a]) 32 dpmin[i][a]=dpmin[i][(i+k)%n]+dpmin[(i+k+1)%n][a]; 33 } 34 else{ 35 if(dpmax[i][a]<dpmax[i][(i+k)%n]*dpmax[(i+k+1)%n][a]) 36 dpmax[i][a]=dpmax[i][(i+k)%n]*dpmax[(i+k+1)%n][a]; 37 if(dpmax[i][a]<dpmax[i][(i+k)%n]*dpmin[(i+k+1)%n][a]) 38 dpmax[i][a]=dpmax[i][(i+k)%n]*dpmin[(i+k+1)%n][a]; 39 if(dpmax[i][a]<dpmin[i][(i+k)%n]*dpmax[(i+k+1)%n][a]) 40 dpmax[i][a]=dpmin[i][(i+k)%n]*dpmax[(i+k+1)%n][a]; 41 if(dpmax[i][a]<dpmin[i][(i+k)%n]*dpmin[(i+k+1)%n][a]) 42 dpmax[i][a]=dpmin[i][(i+k)%n]*dpmin[(i+k+1)%n][a]; 43 44 if(dpmin[i][a]>dpmax[i][(i+k)%n]*dpmax[(i+k+1)%n][a]) 45 dpmin[i][a]=dpmax[i][(i+k)%n]*dpmax[(i+k+1)%n][a]; 46 if(dpmin[i][a]>dpmax[i][(i+k)%n]*dpmin[(i+k+1)%n][a]) 47 dpmin[i][a]=dpmax[i][(i+k)%n]*dpmin[(i+k+1)%n][a]; 48 if(dpmin[i][a]>dpmin[i][(i+k)%n]*dpmax[(i+k+1)%n][a]) 49 dpmin[i][a]=dpmin[i][(i+k)%n]*dpmax[(i+k+1)%n][a]; 50 if(dpmin[i][a]>dpmin[i][(i+k)%n]*dpmin[(i+k+1)%n][a]) 51 dpmin[i][a]=dpmin[i][(i+k)%n]*dpmin[(i+k+1)%n][a]; 52 } 53 } 54 } 55 } 56 int a[60]; 57 int l=0,m=-M; 58 for(int i=0;i<n;i++){ 59 m=max(m,dpmax[i][(i+n-1)%n]); 60 } 61 for(int i=0;i<n;i++) 62 if(dpmax[i][(i+n-1)%n]==m) a[l++]=i+1; 63 cout << m << endl; 64 for(int i=0;i<l;i++){ 65 if(i>0) printf(" %d",a[i]); 66 else printf("%d",a[i]); 67 } 68 printf("\n"); 69 } 70 return 0; 71 }