题目链接:

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。

实现:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXP = , MAXQ = , INF= 0x3f3f3f3f;
int n, p, q, a[MAXP + ], dp[MAXQ + ][MAXQ + ]; int solve()
{
memset(dp, , sizeof(dp));
a[] = ;
a[q + ] = p + ;
for (int j = ; j <= q + ; j++)
{
for (int i = ; i <= q + - j; i++)
{
dp[i][i + j] = INF;
for (int k = i + ; k < i + j; k++)
dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j]);
dp[i][i + j] += a[i + j] - a[i] - ;
}
}
return dp[][q + ];
} int main()
{
cin >> n;
for (int t = ; t <= n; t++)
{
cin >> p >> q;
for (int i = ; i <= q; i++)
{
scanf("%d", &a[i]);
}
cout << "Case #" << t << ": " << solve() << endl;
}
return ;
}

spoj GCJ1C09C Bribe the Prisoners的更多相关文章

  1. GCJ1C09C - Bribe the Prisoners

    GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...

  2. Bribe the Prisoners SPOJ - GCJ1C09C

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

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

  4. 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)

    一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...

  5. GCJ Round 1C 2009 Problem C. Bribe the Prisoners

    区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...

  6. spoj14846 Bribe the Prisoners

    看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...

  7. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  8. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  9. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

随机推荐

  1. 安装mysql 8.0版本时,使用front连接报1251错误或者navicat 连接报错2059解决方案

    这个错误出现的原因是在mysql8之前的版本中加密规则为mysql_native_password,而在mysql8以后的加密规则为caching_sha2_password. 解决此问题有两种方法, ...

  2. Vue源码探究-源码文件组织

    Vue源码探究-源码文件组织 源码探究基于最新开发分支,当前发布版本为v2.5.17-beta.0 Vue 2.0版本的大整改不仅在于使用功能上的优化和调整,整个代码库也发生了天翻地覆的重组.可见随着 ...

  3. linux静默安装Oracle 11g

    ./runInstaller -silent -force -responseFile /home/oracle/software/database/response/orcl_install.rsp ...

  4. I.MX6 Android 5.1.1 下载、编译

    /************************************************************************* * I.MX6 Android 5.1.1 下载. ...

  5. [AHOI 2006] 上学路线

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1266 [算法] 首先 , 用Dijkstra求单源最短路 然后 , 建出这张图G的最 ...

  6. CentOS6.6详细安装教程(图文教程)

    CentOS 6.x最新版本为CentOS 6.6,下面介绍CentOS 6.6的具体安装配置过程,需要的朋友可以参考下说明: Centos6.6 下载地址:thunder://QUFodHRwOi8 ...

  7. 【旧文章搬运】Windbg+Vmware驱动调试入门(二)---Vmware及GuestOS的设置

    原文发表于百度空间,2009-01-08========================================================================== 这一篇是主 ...

  8. 数据库几种Top子句的使用方法

    转自:https://blog.csdn.net/melody_susan/article/details/47011331

  9. 泛型Class<T>和 T. <T>

    private T product; private Class<T> product; 这两个有什么区别呢,查了资料才知道,单独的T 代表一个类型 而 Class<T>代表这 ...

  10. 关于spring boot打出的jar包在Linux中运行

    众所周知, spring boot打出的jar包可以通过 "java -jar xxx.jar"的方式来运行 但是在Linux中, 通过这个命令运行的话会占用该窗口, 当我们 Ct ...