题目描述

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表示有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 (P是总的监狱数)and Q is the number of prisoners to be released(Q是要释放的罪犯个数).
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.

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;const int maxn = 10010;
int A[maxn];
int dp[maxn][maxn];int main(void)
{int P,Q;cin >> P >> Q;A[0]=0;A[Q+1]=P+1;for(int i=1;i<=Q;i++) cin >> A[i];for(int i=0;i<=Q;i++) dp[i][i+1]=0;for(int len=2;len<=Q+1;len++){for(int i=0;i+len<=Q+1;i++){int j=i+len,m=0xFFFFFFF;for(int k=i+1;k<j;k++){m=min(dp[i][k]+dp[k][j],m);}dp[i][j] = m + (A[j]-A[i]+1)-3;}}cout << dp[0][Q+1] << endl; return 0;
}

转载于:https://www.cnblogs.com/zuimeiyujianni/p/9748153.html

Bribe the Prisoners相关推荐

  1. Bribe the Prisoners 菜鸟的记录

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

  2. Bribe the Prisoners SPOJ - GCJ1C09C

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

  3. [区间记忆化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 ...

  4. 贿赂囚犯 Bribe the prisoners (动态规划)

    问题描述: Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line se ...

  5. spoj#GCJ1C09C - Bribe the Prisoners

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  6. 刷题: 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 ...

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

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

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

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

  9. [dp]GCJ 2009 Bribe the Prisoners

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

最新文章

  1. AI一键去纹身,几秒钟让你看见明星「真面目」
  2. C语言SQLite3基本操作Demo
  3. python中导入模块的方式_Python中几种导入模块的方式总结
  4. Graphic Device
  5. 响应QML中的用户输入
  6. go get报错:unrecognized import path “golang.org/x/net/context”…
  7. ClientDataSet建立索引和排序
  8. React Native 实现物流进度信息
  9. python做线性回归统计推断提取参数_概率分析方法与推断统计(来自我写的python书)...
  10. SQL Server判断对象是否存在 (if exists (select * from sysobjects )
  11. 未来集市广告_为什么广告的未来是开放的
  12. java简单窗口程序_一个简单的java窗口程序
  13. css让图片img水平居中-行内元素居中
  14. 《变革中的思索》连载三:展望个人电脑的未来之路
  15. pg事务:隔离级别历史与SSI
  16. mac charles网页代理https使用笔记
  17. C语言入门:计算总成绩和平均成绩
  18. JavaBean为什么需要序列化?
  19. 什么是Galil(加利尔)运动控制卡,它是用来干嘛的呢?galil开发文件dmc32.dll,动态链接库,API
  20. 机器学习基本概念知识汇

热门文章

  1. ICLR 2023:基于 diffusion adversarial representation learning 的血管分割
  2. 关于win11中局域网之内的机器ping不通解决办法
  3. php 教师批改作业代码,【图】请老师改一下_请老师修改文章的敬辞,请老师批改作业用敬语,请老师修改作文怎么说_指标编写互助答疑论坛_理想论坛 - 股票论坛...
  4. 粉红猪小妹peppa pig中英文版209集+218本绘本+音频
  5. 家有网瘾少年不要急,五步教你戒网瘾[为本教育]
  6. Zabbix 02、页面配置
  7. 易经与计算机科学,电子计算机与《易经》有啥关系
  8. 目前电子发票的运转流程图
  9. 动态规划:矩阵连乘问题
  10. 九阴白骨爪(3)Pycharm远程调试设置