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 ...
随机推荐
- HDU5765 Bonds 最小割极
http://acm.hdu.edu.cn/showproblem.php?pid=5765 题意:无向连通图,问每条边在几个最小割极上 思路:用位压形式,表示边的关系.g[1<<i]=1 ...
- 初学AngularJS
最近一直想写个网站,所以在做技术准备.在搜索资料的过程中发现了AngularJS,于是顺藤摸瓜找到了一些资料. 学习的最好途径是:上课. 其次是:看录像: ...
- 单词计数WordCountApp.class
public class WordCountApp { // 可以指定目录,目录下如果有二级目录的话,是不会执行的,只会执行一级目录. private static final String INPU ...
- 二、python 函数
1.定义函数 def max(x,y): if x>y: return x else: return y 如果定义空函数(函数还没想好怎么编写,只是为了让整个代码能够运行起来) def max( ...
- 十个提升你Emacs生产力的高招
转载 十个提升你Emacs生产力的高招 Emacs是世界上最好的编辑器(真的有很多人这么认为).不要以为emacs只是在编写程序时很牛X,其实只要你真正精通了emacs,会发现她几乎在所有用到打字 ...
- j2ee的十三个规范
转载 今天在做连接oracle数据库的时候,感受到了什么是规范.平时听到别人说学习j2ee一定要学习他的十三个规范,大概的知道每个规范是做什么的,每个“接口”是做什么的. 很早就听过 ...
- JAVA中的常见面试题1
1.线程同步的方法的使用. sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法要捕捉InterruptedException异常. wait():使一个线程处于等待状态,并且 ...
- redis的使用
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...
- JavaScript继承方式详解
原文链接 : http://segmentfault.com/a/1190000002440502 js继承的概念 js里常用的如下两种继承方式: 原型链继承(对象间的继承) 类式继承(构造函数间的继 ...
- CodeForces 548A Mike and Fax (回文,水题)
题意:给定一个字符串,问是不是恰好存在 k 个字符串是回文串,并且一样长. 析:没什么好说的,每次截取n/k个,判断是不是回文就好. 代码如下: #include<bits/stdc++.h&g ...