问题描述:

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.

题目大意:
有p个囚犯在监狱,要放出q个囚犯,给出将要释放的囚犯编号(代码中我们使用a[1]~a[p]来存放编号),每放出一个人,他周围的人(两边连续的直到碰到空的监狱或者尽头)都要贿赂1枚金币,目前释放的顺序是不确定的,问最少花费多少金币。
解题思路:
释放某个囚犯后,就把连续的牢房分成了两段,此后这两段牢房就相互独立了。

可以将释放的过程近似看作一棵二叉树(注意每个非叶结点都应计算一次花费),这样就可以把问题视为构造出一棵具有最小花费的二叉树。从小的子树出发,不断扩大子树的规模,即根据最小的子问题组合成稍大一点的子问题(动态规划)。
a[i]代表第i个要释放的囚犯的编号,为了方便处理边界问题,我们把牢房的左墙壁和右墙壁也当作要释放的囚犯。
dp[i][j]表示释放a[i]和a[j]之间应该要释放的囚犯所需要的最少金币数。
初始化:dp[i][i+1]=0(因为a[i]和a[i+1]之间没有待释放囚犯)
动态方程:dp[ i ][ j ] = min( dp[ i ][ j ], dp[ i ][ k ]+dp[ k ][ j ] ) + a[ j ] - a[i] - 2
(a[k]为a[i]和a[j]之间的一名待释放囚犯)

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
int dp[105][105];//ans is dp[0][q+1]
int a[105];
#define INF 0x3fffffff
int main() {int t;cin >> t;int cnt = 0;while (t--) {cnt++;int p, q;cin >> p >> q;//方便解决边界问题  左边界为要释放的第0个囚犯//右边界为要释放的第q+1个囚犯a[0] = 0;a[q + 1] = p + 1;//输入要释放的囚犯for (int i = 1; i <= q; i++) {cin >> a[i];}//dp[i][j]即全部释放a[i]与a[j]囚犯之间要释放的囚犯后,所花费的最小金币//注意不包含边界//dp[0][q+1]即为答案for (int i = 0; i <= q; i++) {dp[i][i + 1] = 0;}//w是跨度 上一步已经对跨度1进行了初始化for (int w = 2; w <= q + 1; w++) {for (int i = 0; i + w <= q + 1; i++) {//j是本次区间的最右端int j = i + w;int tmp = INF;for (int k = i + 1; k < j; k++) {tmp = min(tmp, dp[i][k] + dp[k][j]);}//用于贿赂本区间内的囚犯dp[i][j] = tmp + a[j] - a[i] - 2;}}cout << "Case #" << cnt << ": ";cout << dp[0][q + 1] << endl;}//system("pause");return 0;
}

贿赂囚犯 Bribe the prisoners (动态规划)相关推荐

  1. 编程题-贿赂囚犯(Bribe the prisoners)-动态规划|剪枝

    转载请注明出处:http://blog.csdn.net/Lizo_Is_Me/article/details/43735509 问题描述: 一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,- ...

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

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

  3. 贿赂囚犯 Bribe the prisoners DP

    对于i到j之间我们要去除第k个人. 我们需要i-k之间所有的花费 加上  k-j之间所有的花费  加上 当前需要的花费 前面两部分花费我们dp早就求出了 当前需要的花费 我们是对于当前i到j这一部分, ...

  4. Bribe the Prisoners SPOJ - GCJ1C09C

    滴答滴答---题目链接 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝) Problem In a kingdom there are prison cells (numbere ...

  5. Bribe the Prisoners 菜鸟的记录

    Bribe the Prisoners 题目大意 有一个监狱里有一排牢房1-P,其中住着一些囚犯.现在要释放一些囚犯Q, 当释放一个囚犯的时候,要给两边所有的囚犯每人一枚金币, 直到遇到空牢房或者尽头 ...

  6. [区间记忆化dp入门][Bribe the Prisoners SPOJ - GCJ1C09C][Codeforces Round #505D (rated, Div. 1 + Div. 2, ba]

    Bribe the Prisoners SPOJ - GCJ1C09C 作为这类题代表,f[i][j]代表第i点到第j点单独处理的最值 这题关键:释放某个囚犯后,就把囚犯分成两段,两段互相独立 这类d ...

  7. 刷题: bribe the prisoners(2009 Round 1C C)

    题目描述 In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Ce ...

  8. [dp]GCJ 2009 Bribe the Prisoners

    原文 原作者 lizo_is_me 问题描述: 一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂 ...

  9. Bribe the Prisoners(2009 Round 1C C)

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

最新文章

  1. mysql 导入主键冲突_MySQL 处理插入过程中的主键唯一键重复值的解决方法
  2. Python使用LDAP做用户认证
  3. 超级计算机预测南方下雪,南方九省即将大雪纷飞?超级计算机:可能性增加,但还没有确定...
  4. android 画面,Android 界面组成
  5. 135. 分发糖果(JavaScript)
  6. Date对象在Android和IOS上的兼容
  7. Spring Boot整合MyBatis连接Oracle数据库
  8. 【项目篇】Android团队项目开发之统一代码规范
  9. 【内推】AI独角兽-数美科技-NLP/CV/ASR等开放百余岗位,薪资诱人
  10. Django:数据迁移不生成auth相关表
  11. 斯坦福大学深度学习公开课cs231n学习笔记(3)最优化方法:梯度下降
  12. go home(2)-supprise
  13. echar地图使用小总结
  14. 教务系统自动评教_「四川大学教务处本科登陆系统」四川大学本科教务系统 - 一键评教 - seo实验室...
  15. oracle10g_database安装教程,Oracle Database 10g数据库安装及配置教程
  16. 浊度仪行业调研报告 - 市场现状分析与发展前景预测
  17. python爬虫百度地图_零基础掌握百度地图兴趣点获取POI爬虫(python语言爬取)(基础篇)...
  18. Java/输入圆形半径,求圆形的周长和圆形的面积
  19. Lawnmower E题
  20. 高鲁棒!高实时!慕尼黑工业大学开源RGB-L SLAM!

热门文章

  1. ajax中的trim方法,js中trim函数实例参考
  2. 遗传算法详细介绍以及基于遗传算法和非线性规划函数的寻优算法—MATLAB实现
  3. 密钥安全性讨论之密钥分层管理结构
  4. 什么情况下会导致内存泄露
  5. 在JS中如何去掉千分号
  6. python-opencv 的图像素描
  7. 产业链金融的前世今生
  8. 简易英文统计和加密系统的设计实现(纯C语言实现,包含文件操作、注释多、易理解)
  9. NOIP2013落谷P1311选择客栈题解
  10. Java别在使用普通的照片上传了,你可以使用开源的minio实现图片的上传,方便又简单