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

思路:

dp。

实现:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int MAXP = 10000, MAXQ = 100, INF= 0x3f3f3f3f;
 8 int n, p, q, a[MAXP + 5], dp[MAXQ + 5][MAXQ + 5];
 9
10 int solve()
11 {
12     memset(dp, 0, sizeof(dp));
13     a[0] = 0;
14     a[q + 1] = p + 1;
15     for (int j = 2; j <= q + 1; j++)
16     {
17         for (int i = 0; i <= q + 1 - j; i++)
18         {
19             dp[i][i + j] = INF;
20             for (int k = i + 1; k < i + j; k++)
21                 dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j]);
22             dp[i][i + j] += a[i + j] - a[i] - 2;
23         }
24     }
25     return dp[0][q + 1];
26 }
27
28 int main()
29 {
30     cin >> n;
31     for (int t = 1; t <= n; t++)
32     {
33         cin >> p >> q;
34         for (int i = 1; i <= q; i++)
35         {
36             scanf("%d", &a[i]);
37         }
38         cout << "Case #" << t << ": " << solve() << endl;
39     }
40     return 0;
41 }
时间: 2024-10-06 18:28:07

spoj GCJ1C09C 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

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

编程题-贿赂囚犯(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<

SPOJ 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

SPOJ 3273

传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 1 //SPOJ 3273 2 //by Cydiater 3 //2016.8.31 4 #include <iostream> 5 #include <cstring> 6 #include <ctime> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include