Release of prisoners

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.

[Problem-solving ideas]

Interval dp

A bit difficult to think, the teacher talked about before to understand, to write an article about the problem solution to consolidate.

Dp interval routines: set f [i] [j] is the minimum required interval release i ~ j-th prisoner meat (note, i, j is not the cell number, number of prisoners is released, the following is a [i] array)

Enumeration range of cut-off point k, the transfer equation is:

f [i] [j] = min {f [i] [j], f [i] [k-1] + f [k + 1] [j] + and [j + 1] -a [i-1 ] -1-1}

The latter cook out apart to see, f [i] [k-1] + f [k + 1] [j] + a [j + 1] -a [i-1] -1-1

f [i] [k-1] + f [k + 1] [j], this explanation is not necessary

a [j + 1] -a [i-1] -1 is the j + 1-th to the number of prisoners to be released between the first i-1 th prisoner to be discharged, that is, the number of meat to be sent;

What a -1 is the last, that is the k-th put out a prisoner, do not give him to eat meat (Why not give out meat also

Attach beautiful code:

【code】

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 const int INF=1<<30;
 6 int n,m; 
 7 int f[105][105],a[105];
 8 inline int Min(int a,int b){
 9     return a<b?a:b;
10 }
11 int main(){
12     //freopen("1622.in","r",stdin);
13     //freopen("1622.out","w",stdout);
14     scanf("%d%d",&n,&m);
15     for(register int i=1;i<=m;i++)
16         scanf("%d",&a[i]);
17     sort(a+1,a+m+1);
18     a[m+1]=n+1;
19     for(register int l=1;l<=m;l++)
20         for(register int i=1;i+l-1<=m;i++){
21             int j=i+l-1;
22             f[i][j]=INF;
23             for(register int k=i;k<=j;k++)
24                 f[i][j]=Min(f[i][j],f[i][k-1]+f[k+1][j]+a[j+1]-a[i-1]-1*2);
25         }
26     printf("%d\n",f[1][m]);
27     return 0;
28 }

 

Guess you like

Origin www.cnblogs.com/66dzb/p/11257384.html