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 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, N(N表示有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 (P是总的监狱数)and Q is the number of prisoners to be released(Q是要释放的罪犯个数).
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.

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const int maxn = 10010;
int A[maxn];
int dp[maxn][maxn];

int main(void)
{
    int P,Q;
    cin >> P >> Q;

    A[0]=0;
    A[Q+1]=P+1;

    for(int i=1;i<=Q;i++) cin >> A[i];
    for(int i=0;i<=Q;i++) dp[i][i+1]=0;

    for(int len=2;len<=Q+1;len++)
    {
        for(int i=0;i+len<=Q+1;i++)
        {
            int j=i+len,m=0xFFFFFFF;
            for(int k=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;
}

原文地址:https://www.cnblogs.com/zuimeiyujianni/p/9748153.html

时间: 2024-11-08 05:21:12

Bribe the Prisoners的相关文章

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

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 wind

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

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)-动态规划|剪枝

转载请注明出处:http://blog.csdn.net/Lizo_Is_Me/article/details/43735509 问题描述: 一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房为止.现在要释放a1,a2,...,aQ号囚犯,如何选择释放的顺序,使得使用的金币最少. 思路: 其中很重要的一点:释放了某个囚犯以后,就把连续的牢房分成了没

[SPOJGCJ1C09C] Bribe the Prisoners

题意:有p个犯人,编号从1到p,现有q个犯人可以被释放,但是他两旁的所有犯人看到了觉得很不爽,所以要给1元钱来贿赂他们(请问他在监狱里到哪去花钱),一直到一个空监狱,要求安排q个犯人释放的顺序,使得总代价最小 题解: 区间dp 状态:dp[i][j]表示(i,j)的犯人被释放的最小代价 转移:dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+v[j]-v[i]-2) 初值:dp[i][i+1]=0 1 #include<iostream> 2 #include&l

spoj14846 Bribe the Prisoners

看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有罪犯全部放走的最小花费,于是我们可以将一个区间(i,j)分为(i,k-1),(k+1,j)和k这个点,表示先取走点k的人,这样这个区间就被分成了两个,于是我们便可以转移的.详见代码. 代码 #include<iostream> #include<cstdio> #include<

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M