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 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 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 |
Case #1: 7 |
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)的更多相关文章
- 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 ...
- 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 ...
- Google Code Jam 2016 Round 1C C
题意:三种物品分别有a b c个(a<=b<=c),现在每种物品各选一个进行组合.要求每种最和最多出现一次.且要求任意两个物品的组合在所有三个物品组合中的出现总次数不能超过n. 要求给出一 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- RabbitMQ (三) 发布/订阅 -摘自网络
这篇博客中,我们会做一些改变,就是把一个消息发给多个消费者,这种模式称之为发布/订阅(类似观察者模式). 为了验证这种模式,我们准备构建一个简单的日志系统.这个系统包含两类程序,一类程序发动日志,另一 ...
- Row Border in DataGrid 表格边框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Spring MVC SimpleUrlHandlerMapping example
In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, w ...
- jQuery基础学习3——jQuery库冲突
默认情况下,jQuery用$作为自身的快捷方式. jQuery库在其他库之后导入 在其他库和jQuery库都被加载完毕后,可以在任何时候调用jQuery.noConflict()函数来将变量$的控制权 ...
- Contest 7.21(贪心专练)
这一次都主要是贪心练习 练习地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview Problem APOJ 13 ...
- Unity3D之空间转换学习笔记(一):场景物体变换
该系列笔记基于Unity3D 5.x的版本学习,部分API使用和4.x不一致. 目前在Unity3D中,除了新的UGUI部分控件外,所有的物体(GameObject)都必带有Transform组件,而 ...
- JqueryMobile- 搭建主模板
最近公司要开发手机端的,可是我没学过安卓,然后用HTML5+JQUERYMOBILE也可以做这些手机端的程序,做成个网页,发到网上,免强也行,于是开始了我JQUERYMOBILE的学习. 先放一下主模 ...
- centos6.5下Python IDE开发环境搭建
自由不是想做什么就做什么,而是想不做什么就不做什么. ---摘抄于2016/11/30晚 之前学习了一段时间的Python,但所有部署都在windows上.正赶上最近在学习liux,以后 ...
- Actions 动作集
--> 移动鼠标到指定位置(先触发onMouseOver动作) Actions action = new Actions(driver); WebElement th ...
- PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
, '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...