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.

题目大意:

t 组测试数据,n个人在监狱,要放出m个人,每放出一个人,他周围的人(两边连续的直到碰到空的监狱或者尽头)都要贿赂1个钱,问最少的总花费

解题思路:

记忆化dp,dp(i,j) 表示从 编号 a[i] ~ a[j] 不包含 a[i] 与 a[j] 的子树的花费

状态转移方程 d[i][j]=min(dp(i,k)+dp(k,j)+(a[j]-a[i]-2),d[i][j])

注意,一开始添加哨兵,0号位与n+1号位

 void solve()
{
a[]=;a[m+]=n+;
for(int i=;i<=n;i++)
dp[i][i+]=;
for(int w=;i+w<=m+;i++)
{
for(int i=;i+w<=m+;i++)
{
int j=i+w,t=inf;
for(int k=i+;k<j;k++)
t=min(t,dp[i][k]+dp[k][j]);
dp[i][j]=t+a[j]-a[i]-;
}
}
cout<<dp[][m+]<<endl;
}
 #include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int inf=1e9;
const int ms=;
int a[ms],dp[ms][ms],n,m,p;
int solve(int i,int j)
{
if(dp[i][j]!=inf)
return dp[i][j];
if(i+>=j)
return ;
for(int k=i+;k<j;k++)
dp[i][j]=min(solve(i,k)+solve(k,j)+(a[j]-a[i]-),dp[i][j]);
return dp[i][j];
}
int main()
{
int t;
p=;
cin>>t;
while(t--)
{
cin>>n>>m;
a[]=;a[m+]=n+;
for(int i=;i<=m;i++)
cin>>a[i];
//fill(dp,dp+sizeof(dp)/sizeof(int),inf); 出错
for(int i=;i<=m+;i++)
for(int j=;j<=m+;j++)
dp[i][j]=inf;
int ans=solve(,m+);
cout<<"Case #"<<p++<<": "<<ans<<endl;
}
return ;
}

Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)的更多相关文章

  1. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  2. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  3. Google Code Jam 2016 Round 1C C

    题意:三种物品分别有a b c个(a<=b<=c),现在每种物品各选一个进行组合.要求每种最和最多出现一次.且要求任意两个物品的组合在所有三个物品组合中的出现总次数不能超过n. 要求给出一 ...

  4. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  5. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  6. [Google Code Jam (Qualification Round 2014) ] A. Magic Trick

    Problem A. Magic Trick Small input6 points You have solved this input set.   Note: To advance to the ...

  7. [C++]Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

  8. Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam

    本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...

  9. Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle

    Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...

随机推荐

  1. Chapter4:表达式

    左值和右值 当一个对象被用作右值的时候,用的是对象的值(内容),当对象被用作左值的时候,用的是对象的身份(在内存中的位置). 一个重要的原则是需要右值的地方可以用左值来代替,但是不能把右值当作左值使用 ...

  2. Python之Tkinter模块学习

    本文转载自:http://www.cnblogs.com/kaituorensheng/p/3287652.html Tkinter模块("Tk 接口")是Python的标准Tk ...

  3. Chrome多线程模型

    为什么使用多线程? Chrome的多线程模型主要解决什么问题? 如何实现该问题的解决? 1. 解决问题 Chrome有很多线程,这是为了保持UI线程(主线程)的高响应度,防止被其他费时的操作阻碍从而影 ...

  4. 对JDK,JRE,JVM的理解

    JAVA用到现在还是分不太清楚JDK,JRE,JVM这三者的区别与联系,一直都是模模糊糊的.所以今天整理下此中的关系. 简单说明:我们编写的.java文件经过JDK(JDK的bin目录下javac.e ...

  5. WebService学习之四:关于JAX-WS 注释

    基于 XML 的 Web Service 的 Java API"(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注释 ...

  6. Android实例-程序切换到后台及从后台切换到前台

    相关资料: http://www.delphitop.com/html/Android/2933.html 程序包下载: http://download.csdn.net/detail/zhujian ...

  7. 随便看看My97DatePicker源码J方法

    如果有一个路径是写错的并且这个路径是写在前面,那么相关的css文件你就别想引进来了 <script language="javascript" type="text ...

  8. iomanip.h

    http://baike.baidu.com/link?url=zuNLgcUVylhUYYefyV13F38NChIMx8nnCEWV5zkkTQMrrSdKPxUERZuydSHtp6sXukWv ...

  9. [iOS基础控件 - 6.10.5] UIApplication

    A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...

  10. HTML5实现动画三种方式

    编者注:作者以一个运动的小车为例子,讲述了三种实现HTML5动画的方式,思路清晰,动画不仅仅是canvas,还有css3和javascript.通过合理的选择,来实现最优的实现. PS:由于显卡.录制 ...