Problem
In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called “neighbours.” A wall with a window separates adjacent cells, and neighbours can communicate through that window.
All prisoners live in peace until a prisoner is released. When that happens, the released prisoner’s neighbours find out, and each communicates this to his other neighbour. That prisoner passes it on to his 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 has been released will angrily break everything in his cell, unless he is bribed 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 is initially occupied by exactly one prisoner, and that only one prisoner can be released per day. Given the list of Q prisoners to be released in Q days, find the minimum total number of gold coins needed as bribes if the prisoners may be released in any order.
Note that each bribe only has an effect for one day. If a prisoner who was 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 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 and Q is the number of prisoners to be released.
This will be followed by a line with Q distinct cell numbers (of the prisoners to be released), space separated, sorted in ascending order.
Output
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.
Large dataset
1 ≤ P ≤ 10000
1 ≤ Q ≤ 100
Sample
Input
2
8 1
3
20 3
3 6 14
Output
Case #1: 7
Case #2: 35
Note
In the second sample case, you first release the person in cell 14, then cell 6, 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 will be 19 + 4 + 13 = 36.
#include<iostream>
#include<cstring>
using namespace std;
const int MAXQ = 110;
const int INF = 1 << 30;
int q,p,d[MAXQ][MAXQ];//d[i][j] 表示释放从i号犯人到j号犯人(开区间)需要最少钱
int pri[MAXQ];
int main(){
int T,i,k,j;
scanf("%d",&T);
for(k = 1;k <= T; ++k){
scanf("%d%d",&p,&q);
for(j = 1;j <= q; ++j)
scanf("%d",pri + j);
pri[0] = 0;
pri[q + 1] = p + 1;
memset(d,0,sizeof(d));
for(int w = 2;w <= q + 1; ++w){
for(i = 0;i + w <= q + 1; ++i){
j = i + w;
int ans = INF;
for(int t = i + 1;t < j; ++t)
ans = min(ans,d[i][t] + d[t][j]);
d[i][j] = ans + pri[j] - pri[i] - 2;
}
}
printf("Case #%d: %d\n",k,d[0][q + 1]);
}
return 0;
}
此题和锯木头不一样呐,锯木头只给出了各段长度,当然可以任意组合因此使用哈夫曼树。
此题被释放的犯人的位置都是确定的,因此该法行不通。
在某段连续的区间中,某个犯人被释放后,它的两边再释放就完全独立了,因此总的钱最少就是要在该区间内,两段的钱的和最少,即满足最优子结构条件。当然不管怎么划分,首次划分的钱总是一样多的(闭区间减一),即满足无后效性。
总的来说,还是判断这题是否适合用dp来解比较难,题目做了也有近20道了吧,还不是事后诸葛亮,哎~