Polygon Game (Classic ring DP)

Source: Internet
Author: User

Describe
A polygon that begins with n vertices. Each vertex is given a positive integer value, and each edge is given an operator "+" or "*". All edges are numbered from 1 to n sequentially by integer.
Now to play a game, the game has a total of n steps:
1th step, select an edge and delete it
Then n-1 step, each step is performed as follows: (1) Select an edge E and 2 vertices v1 and v2 connected by E, and (2) Assign the integer value of vertex v1 and v2 to the new vertex by replacing the edge E with a new vertex and the 2 vertices v1 and v2 connected by E.
Finally, all edges are deleted, only one vertex is left, and the game is over. The game score is the integer value on the remaining vertex. So what's the maximum of this integer value?
Input
The number of vertices of the first behavior Polygon N (n≤50), followed by n lines, each behavior an integer and a character, an integer is a positive integer value on the vertex, and the character is the operator "+" or "*" on the edge of the vertex to the next vertex (the last character is the operator on the edge of the first vertex).
Output
The output is only an integer, which is the maximum value calculated by the game.
Exercises
A very classic ring DP
First we can see that the number of deleted edges must be n-1, so that the sequence of operations is actually a chain containing n vertices.
This graph can be divided into a chain of length 1,2,3...N, where the length refers to the number of fixed points contained in the chain.
We set f[i][j][0] to the maximum value of the chain with the vertex I as the starting point and the length J.
F[I][J][1] is the minimum value of the chain with the vertex I as the starting point and the length J.
Then our final answer is f[i][n][0] (1<=i<=n);
Now consider the transfer.
Transfer is also a very classic way to use.
The enumeration is broken from the edge of the current chain, and since we have calculated the broken two strands, we have satisfied the optimal substructure.
It is important to note that if the operator on the edge of the break is multiplication sign, there may be negative negative results in the data
The minimum value for sub-chain A is x maximum is Y, sub-chain B has a minimum value of XX, and the maximum value is yy
The maximum value of the chain is Max (X*xx, X*yy, Y*xx, y*yy);
Min values are min (x*xx, X*yy, Y*xx, y*yy);
Because it is a ring, it requires modulo n when calculating the vertex;

#include <iostream>usingnamespaceStdintn,v[1001],f[101][101][2],temp,a,b,c,d,ans,tt;char op[1001],ch;intMain () {cin>>n; for(intI=1; i<=n;i++) cin>>v[i]>>op[i]; for(intI=1; i<=n;i++) f[i][1][0]=f[i][1][1]=v[i]; for(intj=2; j<=n;j++) for(intI=1; i<=n;i++) for(intk=0; k<=j-2; k++) {temp= (i+k)%n;if(temp==0) Temp=n; Tt= (i+k+1)%n;if(tt==0) Tt=n; CH=OP[TEMP];if(ch==' + ') {f[i][j][1]=Max(f[i][k+1][1]+f[tt][j-k-1][1],f[i][j][1]); f[i][j][0]=min(f[i][k+1][0]+f[tt][j-k-1][0],f[i][j][0]); }if(ch==' * ') {a=f[i][k+1][1]*f[TT] [j-k-1][1]; b=f[i][k+1][1]*f[TT] [j-k-1][0]; c=f[i][k+1][0]*f[TT] [j-k-1][1]; d=f[i][k+1][0]*f[TT] [j-k-1][0]; f[i][j][1]=Max(Max(Max(A, B),Max(c,d)), f[i][j][1]); f[i][j][0]=min((min(A, B),min(c,d)), f[i][j][0]); }} ans=-100000000; for(intI=1; i<=n;i++) ans=Max(ans,f[i][n][1]); Cout<<ans;}

Polygon Game (Classic ring DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.