I. Question
A polygon is provided to meet the following requirements:
1. Each vertex is a numerical value.
2. Each side is a symbol
We disconnect an edge to form a chain composed of values and symbols, and then calculate the value of this chain.
1. You can choose to disconnect any side.
2. When you evaluate the chain value, you do not have to select any order of priority based on the operator.
The requirement of the question is to obtain the maximum value.
Ii. Example
Iii. Analysis
1. For example, save the graph information as follows:
Number of vertices: REAL_SIZE = 3
Vertex: v [3] = {1, 2, 3}
Edge: op [3] = {'+', 'x', '+ '}
2. If we disconnect from edge I, a chain is formed.
V [I], op [I + 1], v [I + 1], op [I + 2]... v [I + s-1], op [s], v [I + s]... op [I-1], v [I-1]
Calculate the maximum value. The maximum value required by the question is the maximum value of each chain that can be obtained after each edge is disconnected.
3. For the convenience of computing, let's note:
P [I, j] indicates the link starting from vertex I, including j vertices.
M [I, j, 0] indicates the minimum value of the chain.
M [I, j, 1] indicates the maximum value of the chain.
In this way, all m [I, REAL_SIZE, 1] of I from 0 to REAL_SIZE is the result of the question.
4. disconnect p [I, j] At op [I + s] to form two chains: p [I, s] and p [I + s, j-s].
Obtain the minimum and maximum values of p [I, s] and p [I + s, j-s], and then obtain the maximum and minimum values of p [I, j] disconnected at s.
When s starts from 1 to J-1, get the most value of the corresponding disconnection method, from these most values select the minimum and maximum as m [I, j, 0] and m [I, j, 1]
4. The Code is as follows:
# Include <stdio. h> </p> <p> // ------------------------ predefined and global variable values <br/> # define MAX_VETEX_SIZE 20 // maximum number of vertices that the program can accept </p> <p> int REAL_SIZE; // actual number of vertices in the Program <br/> int m [MAX_VETEX_SIZE] [MAX_VETEX_SIZE + 1] [2]; // Save the maximum and minimum values (the second dimension indicates the number of vertices in the chain) <br/> int v [MAX_VETEX_SIZE]; // vertex (value) <br/> char op [MAX_VETEX_SIZE]; // edge (Symbol) </p> <p> // -------------------------- initialize the m array ------------------------------------- ---- <Br/> void init_m () {<br/> int I; <br/> for (I = 0; I <REAL_SIZE; I ++) {<br/> m [I] [1] [0] = v [I]; // The link has only one vertex I <br/> m [I] [1] [1] = v [I]; <br/>}</p> <p> // ----------------------- returns the highest value of the four counts. <br/> void getMin_Max (int n1, int n2, int n3, int n4, int * min, int * max) {<br/> // returns the maximum and minimum values of the four operands <br/> * min = (n1> n2 )? (N2> n3 )? (N3> n4? N4: n3) :( n2> n4? N4: n2) <br/> :( (n1> n3 )? (N3> n4? N4: n3) :( n1> n4? N4: n1); </p> <p> * max = (n1 <n2 )? (N2 <n3 )? (N3 <n4? N4: n3) :( n2 <n4? N4: n2) <br/> :( (n1 <n3 )? (N3 <n4? N4: n3) :( n1 <n4? N4: n1); <br/>}</p> <p> // --------------------- obtain the maximum and minimum values with breakpoints ------------------------------------ <br/> void minMax (int I, int s, int j, int * minf, int * maxf) {<br/> // p [I, j] the maximum and minimum values of maxf disconnected from s <br/> // int e [4]; // ac, ad, bc, bd value <br/> int a, B, c, d, r; <br/> a = m [I] [s] [0]; // a stores the minimum value of the chain formed from the I to I + s vertex <br/> B = m [I] [s] [1]; </p> <p> r = (I + s) % REAL_SIZE; </p> <p> c = m [r] [j-s] [0]; <br/> d = m [r] [j-s] [1]; <br/> // printf ("I = % d; s = % d; j = % d; r = % d/n ", I, s, j, r); <br/> if (op [r] = '+ ') {// process the plus sign <br/> * minf = a + c; <br/> * maxf = B + d; <br/>}< br/> else {// process multiplication <br/> getMin_Max (a * c, a * d, B * c, B * d, minf, maxf); // obtain the ac, ad, bc, maximum and minimum values of bd <br/>}</p> <p> // ---------------------------- obtain the result struct <br/> int Poly_Max () {<br/> int minf, maxf, temp; <br/> int I, j, s; <br/> for (j = 2; j <= REAL_SIZE; j ++) {// control the number of vertices in each chain <br/> for (I = 0; I <REAL_SIZE; I ++) {// control the start position of the vertex in each chain <br/> for (s = 1; s <j; s ++) {<br/> minMax (I, s, j, & minf, & maxf); <br/> if (m [I] [j] [0]> minf) m [I] [j] [0] = minf; <br/> if (m [I] [j] [1] <maxf) m [I] [j] [1] = maxf; <br/>}< br/> // at this time, the maximum and minimum values of all s corresponding to the vertex from I to I + j are obtained. <br/>}< br/>} </p> <p> // find all links with REAL_SIZE vertices, <br/> temp = 0; <br/> for (I = 1; I <REAL_SIZE; I ++) {<br/> if (m [I] [REAL_SIZE] [1]> m [temp] [REAL_SIZE] [1]) {<br/> temp = I; <br/>}</p> <p> return m [temp] [REAL_SIZE] [1]; <br/>}</p> <p> // --------------------------------- main function callback <br/> void main () {<br/> int I = 0; </p> <p> scanf ("% d", & REAL_SIZE); // number of user input vertices <br/> for (I = REAL_SIZE-1; I> = 0; I --) {// vertex and edge information <br/> scanf ("% d % c", & v [I], & op [I]); // input sample: <br/>}// 3 <br/> // 1x3 + 2 + <br/> init_m (); </p> <p> printf ("% d/n", Poly_Max (); // The output is 9 <br/>}