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 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.
解题:这个题目蛮有意思啊。。。本来是进行拆解的,逆向成填充,这样就能像求最优二叉搜索树那样进行动态规划求取了
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int dp[maxn][maxn],d[maxn];
int main(){
int T,p,q,cs = ;
scanf("%d",&T);
while(T--){
scanf("%d %d",&p,&q);
for(int i = ; i <= q; ++i)
scanf("%d",d+i);
d[] = ;
d[++q] = p+;
for(int i = ; i < maxn; ++i) dp[i][i] = ;
for (int w = ; w <= q; ++w){
for(int i = ,j = w; j <= q; ++i,++j){
dp[i][j] = INF;
for(int k = i + ; k < j; ++k)
dp[i][j] = min(dp[i][k]+dp[k][j]+d[j]-d[i]-,dp[i][j]);
}
}
printf("Case #%d: %d\n",cs++,dp[][q]);
}
return ;
}
GCJ1C09C - Bribe the Prisoners的更多相关文章
- spoj GCJ1C09C Bribe the Prisoners
题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to ...
- Bribe the Prisoners SPOJ - GCJ1C09C
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- 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. ...
- 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)
一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...
- GCJ Round 1C 2009 Problem C. Bribe the Prisoners
区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...
- spoj14846 Bribe the Prisoners
看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- 囚徒问题(100 prisoners problem)的python验证
密码学课上老师介绍了这样一个问题,囚徒问题(100 prisoners problem):一百个囚徒被关在牢房里,典狱长给他们最后一次机会,100人依次进入一个有100个抽屉的牢房,每个抽屉置乱放入1 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
随机推荐
- iOS9适配小结
前言 最新公布的app版本号适配了iOS9.总结一下适配过程的几个要点. Bitcode iOS9此番推出了新的特性:Bitcode,关于Bitcode的资料大家能够在网上找.Bitcode要求pro ...
- Android-Universal-Image-Loader 的使用说明
这个图片异步载入并缓存的类已经被非常多开发人员所使用,是最经常使用的几个开源库之中的一个,主流的应用,随便反编译几个火的项目,都能够见到它的身影. 但是有的人并不知道怎样去使用这库怎样进行配置,网上查 ...
- BootStrap有用代码片段(持续总结)
> 如题.持续总结自己在使用BootStrap中遇到的问题.并记录解决方法.希望能帮到须要的小伙伴 1.bootstrap上下布局.顶部固定下部填充 应用场景:经典上下布局中,顶部导航条固定,下 ...
- CSS的flex布局和Grid布局
一.什么是 flex 布局 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这 ...
- require(): open_basedir restriction in effect. File
新安装的 lnmp 环境,将项目放上报 require(): open_basedir restriction in effect. File 的错误! 错误日志显示,访问脚本不在 open_base ...
- 用 while 循环做个小游戏
import random #可输入次数 flag = 0 #生成一个1到10之间的随机整数 res = random.randint(1,10) #判读三次输入机会 while(flag<3) ...
- [CEOI2007]树的匹配Treasury(树形DP+高精)
题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...
- laravel中soapServer支持wsdl的例子
最近在对接客户的CRM系统,获取令牌时,要用DES方式加密解密,由于之前没有搞错这种加密方式,经过请教了"百度"和"谷歌"两个老师后,结合了多篇文档内容后,终于 ...
- Unity 编辑器学习(四)之 静态游戏物体
一.Static GameObjects 关于静态对象的信息往往可以预先在编辑器里计算,不需要实时计算,进而优化性能,明显降低DrawCall. 各种Static说明: Lightmapping: 用 ...
- Java Web学习总结(20)——基于ZooKeeper的分布式session实现
1. 认识ZooKeeper ZooKeeper-- "动物园管理员".动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而不是像走在原始 ...