[P1880] [NOI1995] combined gravel

Title Description

Playground around a circular stones piled placed N, Stone is to have order to merge into a pile. Each time only a predetermined selected adjacent stack 2 into a new pile and the new pile of stones number, remember that the combined score.

Test design an algorithm to calculate the N stones piled into a stack the minimum combined score and maximum score.

Input Format

A first data line test positive integer N, 1≤N≤100, N stones piled expressed. The second row has the number N, respectively, represent the number of each pile of stones.

Output Format

The output common line 2, line 1 is the minimum score, the maximum score of the second row.

Sample input and output

Input # 1
4
4 5 9 4
Output # 1
43
54


dalao of题解: (Wsqtql Pasentopasentopasento)

Probably thinking:

This problem does not look good to start with sub-sub (dp konjac distress)

Read a lot of problem solution summed up the idea: first copy all the stones again, to simulate the ring, then the interval DP. In DP, the direct enumeration of the blade position, the starting point and the length of the interval will waste a lot of time, because the blade is not necessarily just fall within the range. Therefore, the length and the starting point of the first enumeration, enumeration and then go to the location of the knife according to the known range can effectively solve this problem.

(This question regarded the board questions intervals DP, the code will be placed here)

Code:

#include<iostream>
#include<cstdio> 
#include<cmath>
#include<cstring>
using namespace std;
int Fmin[201][201];
int Fmax[201][201];
int sum[1001];
int num[1001];
int n;
void read(int &x)
{
    char c=0;
    x=0;
    while(!isdigit (C)) 
    C = getchar ();
     the while (isdigit (C)) 
    X = X * 10 + the C- ' 0 ' , C = getchar (); 
} // Fast Read (only for positive numbers) 
int main () 
{ 
    Memset (the Fmin, 0x3f3f3f , the sizeof (the Fmin)); 
    Read (n-); 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        Read (NUM [I]); 
        NUM [I + n-] = NUM [I]; // prefix, and the cost of the process may be required to quickly merge 
        the Fmin [I] [I] = 0 ; 
    }
    for ( int I = . 1 ; I <= 2 * n-; I ++ ) 
    { 
        SUM [I] = SUM [I- . 1 ] + NUM [I]; 
        the Fmin [I + n-] [I + n-] = 0 ; 
    } 
    for ( int P = . 1 ; P <= n-; P ++) // enumeration interval length 
    {
     for ( int I = . 1 ; P + I <= 2 * n-; I ++) // interval starting 
    {   
         int POS = I + p- 1 ; // end in this 
        for ( int k=i;k<pos;k++)//枚举刀口 
        {
            Fmin[i][pos]=min(Fmin[i][pos],Fmin[i][k]+Fmin[k+1][pos]+sum[pos]-sum[i-1]);
            Fmax[i][pos]=max(Fmax[i][pos],Fmax[i][k]+Fmax[k+1][pos]+sum[pos]-sum[i-1]);
            
        }
    }
    } 
     int Max=-1;
     int Min=0x3f3f3f;
     for(int i=1;i<=2*n;i++)
     {
         Max=max(Max,Fmax[i][i+n-1]); // Since cyclic, i + n-1 is a combined method of precisely 
         Min = min (Min, the Fmin [I] [I + N- . 1 ]); 
     
     } 
     COUT << endl << Min < < Max;
     return  0 ; 
}

Look at the questions right. .

Title Description

Caima Kingdom has a strange prison, jail that a total of P cells, the cells are lined up next to the i th first i + 1 (except the last). Right now the cell is full.

Superiors made a list of the next release, demanding the release of a person on the list every day. This spooked the guards because the guards knew, P in individual cells can be another messenger between now. If someone left, then the original and this one can say the words of the people will be very angry, causing them that day would have been yelled at and made caretaker headache. If these people want to get angry to eat meat, they will be quiet.

Input Format

The first line of two numbers P and Q, Q represents the number of the release list;

The number of Q the second line, which represents the people to be released.

[Data] scale

To 100% of the data 1≤P≤1000; 1≤Q≤100; Q≤P; and 50% of the data 1≤P≤100; 1≤Q≤5

Output Format

Only one line represents the minimum number of give people send meat.

Sample input and output

Input # 1
20 3
3 6 14
Output # 1
35


Description / Tips

[Sample Description]

14 to release the criminal in prison, to give 131 prison No. 15 and No. 20 to 19 prisons feed meat; 6 releases the offenders in prisons, to give 51 No. 7-13 and prison No. 12 prisons feeding meat; 3 finally released offenders in prisons, to give a 2 to 4 prisons and jails 5 4 feed meat.

 
 

 Probably thinking:

I thought about an hour. . How I do not want to come out to how to write. .

Because each state has its after-effect, it is also positive for the sake how the solution does not come out. . .

After reading about the solution to a problem big brother, the man regarded as the breakpoint to be released, will be divided into individual p q + 1 intervals, the interval seeking to merge a range of the minimum required, can be deemed to be incorporated stones, suddenly Wu Tong ...... However, to be noted that some of the details into q + 1 at intervals. .

Epiphany

Epiphany! ! ! !

(Code point right here)

Guess you like

Origin www.cnblogs.com/Daz-Os0619/p/11693455.html