Code Jam 2009 Round1C Problem C. Bribe the Prisoners —— 区间DP

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 Qdays, 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, NN 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.

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 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.

典型的区间DP,题意是给你一排p个牢房,最初的时候每个牢房中有一个人,给出一个A数组表示我们要释放的人的位置,我们设定释放一个囚犯,我们要给他左边直到空牢房或墙和右边直到空牢房或墙的每个囚犯一枚金币。请你设计释放的顺序使得总花费最小,输出最小花费。

思路时释放某个人的花费为他左边连续的人数+他右边连续的人数,我们可以按此递推。

我们用dp[i][j]表示释放从第i个人到第j个人的最小花费(不包括端点)。然后进行O(n^3)的枚举即可。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , p , q;
int a[MAXN];
int dp[MAXN][MAXN];
void solve()
{
    FOR(i , 0 , q + 1)dp[i][i + 1] = 0;
    FORR(w , 2 , q + 1)for(int i = 0 ; i + w <= q + 1 ; i++)
    {
        int j = i + w , t = inf;
        FOR(k , i + 1, j)
        {
            t = min(t , dp[i][k] + dp[k][j]);
        }
        dp[i][j] = t + a[j] - a[i] - 2;
    }
}
int main()
{
    freopen("C-large-practice.in" , "r" , stdin);
    freopen("C-large-practice.out" , "w" , stdout);
    int t;
    cin >> t;
    FORR(kase ,1 , t)
    {
        scanf("%d%d",&p , &q);
        FORR(i , 1 , q)
        {
            scanf("%d" , &a[i]);
        }
        a[0] = 0;
        a[q + 1] = p + 1;
//        FOR(i , 0 , q + 2)cout << '*'<< a[i] << endl;
        solve();
//        FOR(i , 0 , q + 2)FOR(j , i + 1 , q + 2)cout << '*' << dp[i][j] << endl;
        printf("Case #%d: %d\n" , kase , dp[0][q + 1]);
    }
    return 0;
}


你可能感兴趣的:(ACM,区间DP)