GCJ 2009 Round 1C Bribe the Prisoners

Bribe the Prisoners

no tags

   

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

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.

题意:

一个监狱里有P个并排着的牢房。从左至右依次编号为1,2,...,P。最初所有的牢房里都住着一个囚犯。相邻的两个牢房之间可以互通信息。

现在要释放一些囚犯。如果释放某个牢房里的囚犯,其相邻的牢房里的囚犯就会知道,因而发生暴动。所以,释放某个囚犯时,必须要贿赂两旁相邻牢房的囚犯一枚金币。另外,为了防止释放的消息在相邻牢房间传开,不仅两旁直接相邻的牢房,所有可能听到消息的囚犯,即直到空牢房为止或直到监狱两端为止,此间的所有囚犯都必须给一枚金币。

现在要释放Q名囚犯。如果选择所需金币数量尽量少的顺序释放,最少需要多少枚金币?

分析:DP算法

dp[i][j]表示的是,将从a[i]号囚犯到a[j]号囚犯(不含两端的囚犯)的连续部分里的所有囚犯都释放时,所需的最少金币总数。

为了更方便的处理两端的情况,我们把左端当成0号囚犯,右端当成Q + 1号囚犯。这样,dp[0][Q + 1]就是答案。

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
const int INF = 10000000;

int P, Q, a[maxn];    //A中保存输入数据,下标从1开始
int dp[maxn][maxn];   //dp[i][j] := 释放(i, j)所需的金币
int main()
{
    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; cas++){
        scanf("%d%d", &P, &Q);
        for (int i = 1; i <= Q; i++){
            scanf("%d", &a[i]);
        }

        //为了方便,将两端加入a中
        a[0] = 0;
        a[Q + 1] = P + 1;

        //初始化
        for (int q = 0; q < Q; q++){
            dp[q][q + 1] = 0;
        }

        //从短的区间开始填充dp
        for (int w = 2; w <= Q + 1; w++){
            for (int i = 0; i + w <= Q + 1; i++){
                //计算dp[i][j]
                int j = i + w, t = INF;
                //枚举最初释放的囚犯,计算最小的费用
                for (int k = i + 1; k < j; k++){
                    t = min(t, dp[i][k] + dp[k][j]);
                }

                //最初的释放还需要与所释放囚犯无关的a[j] - a[i] - 2枚金币
                dp[i][j] = t + a[j] - a[i] - 2;
            }
        }
        printf("Case #%d: %d\n", cas, dp[0][Q + 1]);
    }
    return 0;
}
时间: 2024-11-03 05:41:11

GCJ 2009 Round 1C Bribe the Prisoners的相关文章

Google Code Jam 2009, Round 1C 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

GCJ 2015 Round 1C B. Typewriter Monkey

用最多需要的香蕉数减去目标串出现的概率就行啦.... 如何求出现的概率? 每个字符出现的概率乘起来--再乘以目标串能摆的位置个数-- Problem Your publishing house has decided to use monkeys randomly typing at keyboards to write great works of literature. You are the supervisor for one monkey with a keyboard contain

GCJ 2015 Round 1C C. Less Money, More Problems

如果现在能够组成1...x 的面值,加上一种 x+1 面值的纸币,就能组成 1 ... x + C * (x + 1)的面值. 因为如果面值是 k < (C+1)*(x + 1),我们可以用 k / (x + 1) 张 x+1 面值的纸币,并用原来的纸币组成 k % (x + 1),就得到了 k . 每次添加不能被表示的最小面值的纸币是最优的. Problem Up until today, the nation you live in has used D different positive

GCJ1C09C - Bribe the Prisoners

GCJ1C09C - Bribe the Prisoners 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 windo

TCO 2014 Round 1C 概率DP

TCO round 1C的 250 和500 的题目都太脑残了,不说了. TCO round 1C 950 一个棋子,每次等概率的向左向右移动,然后走n步之后,期望cover的区域大小?求cover,肯定就是dp[l][r][n], 走了n步之后,左边cover了l,右边cover了r. 一开始DP没有搞清楚,这个要画一下图就更清楚了. 转移方程就是概率的传递方向. 1: double dp[505][505][2]; // l,r,n steps unsed; 2: class RedPain

GCJ Round 1C 2016 题解

ASenate Evacuation B Slides C Fashion Police A.Senate Evacuation #include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define For

GCJ——Crazy Rows (2009 Round 2 A)

题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: 假如每一行的1的个数都是不相同的,即,最终答案中的矩阵是唯一的,这就相当于求对给定数组冒泡排序需要几次交换一样.但显然,题目没有如此保证. 方法是贪心法:(策略不给出证明) 从第一行到最后一行依次满足,因为可以满足前面行的也一定可以满足后面的,所以每次只需要找到可以满足当前行的最近的就可以了. 预处理最后

spoj GCJ1C09C Bribe the Prisoners

题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: 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 wi

Bribe the Prisoners

题目描述 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 ne