Title Description
In a kingdom there is prison cells (numbered 1 to P) built to form a straight line segment. Cells number I and i+1 is adjacent, and prisoners in adjacent cells is called "neighbours." A wall with a window separates adjacent cells, and neighbours can communicate through the that window.
All prisoners live in peace until a prisoner is released. When that's happens, the released prisoner ' s neighbours find out, and each of the communicates this to its other neighbour. That's prisoner passes it on to He other neighbour, and so on until they reach a prisoner with no other Neighbour (because he is in cell 1, or in cell P , or the other adjacent cell is empty). A prisoner who discovers that another prisoner have been released would angrily break everything in his cell, unless he's B Ribed with a gold coin. So, after releasing a prisoner in cell a , all prisoners housed on either side of cell A -until cell 1, cell P or An empty cell-need to be bribed.
Assume that each prison cell was initially occupied by exactly one prisoner, and so only one prisoner can be released per Day. Given the list of Q prisoners to being released in Qdays, find the minimum total number of gold coins Neede D as bribes if the prisoners is released in any order.
Note that each bribe is only have a effect for one day. If a prisoner who is bribed yesterday hears about another released prisoner today, then he needs to be bribed again.
Input
The first line of input gives the number of cases, N (n means there are n test examples). N test Cases follow. Each case consists of 2 lines. The first line is formatted as
P Q
where P is the number of prison cells (P is the total prison count) and Q is the number of prisoners to being released (Q is the count of offenders to release).
This would be followed by a line with Q distinct cell numbers (of the prisoners to be released), space separated, sorted in Ascending order.
Output (export format)
For each test case, output one line in the format
Case #X: C
Where X is the case number, starting from 1, and C is the minimum number of gold coins needed as bribes.
Limits
1 ≤ N ≤100
Q ≤ P
Each cell number is between 1 and P, inclusive.
Small DataSet
1 ≤ P ≤100
1 ≤ Q ≤5
Large DataSet
1 ≤ P ≤10000
1 ≤ Q ≤100
Sample
Input
|
Output
|
2 8 1 3 20 3 3 6 14
|
Case #1: 7 Case #2: 35
|
Note
In the second sample case, you first release the "person" in cell and then cell 6 and then cell 3. The number of gold coins needed is 19 + 12 + 4 = 35. If you instead release the person in cell 6 first, the cost would be 19 + 4 + 13 = 36.
#include <algorithm>#include<iostream>#include<cstdio>#include<cmath>using namespacestd;Const intMAXN =10010;intA[MAXN];intDP[MAXN][MAXN];intMainvoid){ intp,q; CIN>> P >>Q; a[0]=0; A[q+1]=p+1; for(intI=1; i<=q;i++) Cin >>A[i]; for(intI=0; i<=q;i++) dp[i][i+1]=0; for(intlen=2; len<=q+1; len++) { for(intI=0; i+len<=q+1; i++) { intj=i+len,m=0xFFFFFFF; for(intk=i+1; k<j;k++) {m=min (dp[i][k]+dp[k][j],m); } Dp[i][j]= m + (a[j]-a[i]+1)-3; }} cout<< dp[0][q+1] <<Endl; return 0;}
Bribe the prisoners