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 hisother 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, NN 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 
 
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.

题解:

  区间dp水题,套路状态dp[l][r]表示将l到r的点全部搞完的最小代价,因为这个状态转移会重复所以考虑加一个限制条件,不包括两个端点。

  那么我们就套路枚举断点,暴力转移,dp[l][r]=min(dp[l][k]+dp[k][r]+w[r]-w[l]-2)减去2是因为不算端点,要加两个关键点0和n,答案就是dp[0][n](因为不考虑端点),我是写的记忆化搜索,自然一点,如果for的话先枚举一个len就可以了。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define ll long long
#define MAXN 500
using namespace std;
int n,q,a[MAXN],w[MAXN],b[MAXN][MAXN];
ll dp[MAXN][MAXN]; ll dfs(int l,int r){
if(b[l][r]) return dp[l][r];
if(l+==r) return ;
b[l][r]=;
ll tmp=<<;
for(int i=l+;i<=r;i++){
tmp=min(tmp,dfs(l,i)+dfs(i,r)+w[r]-w[l]-);
}
dp[l][r]=tmp;
return tmp;
} int main()
{
int t;cin>>t;int Case=;
while(t--){
scanf("%d%d",&n,&q);
memset(b,,sizeof(b));
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(w,,sizeof(w));
for(int i=;i<=q;i++) scanf("%d",&a[i]);
a[++q]=,a[++q]=n+;
sort(a+,a+q+);
for(int i=;i<=q;i++) w[i]=a[i];
int k=unique(w+,w+q+)-w-;
for(int i=;i<=q;i++) a[i]=lower_bound(w+,w+k+,a[i])-w;
printf("Case #%d: %lld\n",++Case,dfs(,k));
}
return ;
}

最新文章

  1. truncate table语句和delete table语句的区别
  2. SQL Server调优系列进阶篇(如何维护数据库索引)
  3. Windows 部署 Redis 群集
  4. navicat 连接sqlserver提示要安装 sql server native client
  5. hdu 1003 Max sum(简单DP)
  6. xampp 修改mysql 密码
  7. 【原】Object 异常静态
  8. hadoop 分片与分块,map task和reduce task的理解
  9. mysql进阶(十五) mysql批量删除大量数据
  10. ORACLE中RECORD、VARRAY、TABLE的使用详解(转)
  11. 9款国内外垂直领域的在线作图工具:那些可以替代Visio的应用!【转】
  12. Linux 系统TCP连接内存大小限制 调优
  13. Log4j2使用笔记
  14. Leetcode——198. 打家劫舍
  15. 71. Simplify Path(M)
  16. 2nd 词频统计更新
  17. appium+python自动化47-首次打开app权限弹窗问题
  18. node.js---sails项目开发(4)---配置MongoDB数据库连接
  19. 二分图最大权值匹配 KM算法 模板
  20. Docker容器-入门级

热门文章

  1. 纯JS实现在一个字符串b中查找另一个字符串a出现的所有位置,并且不使用字符串的方法(递归)
  2. spring boot发送其他邮件
  3. Python中使用moviepy进行视频分割
  4. charles 远程映射到URL地址
  5. Hadoop 之 分布式缓存的原理和方法——DistributedCache
  6. 不fq安装 golang tools
  7. Spring MVC-从零开始-如何访问静态资源
  8. 使用Espresso测试记录
  9. java8 两个时间比较
  10. pyinstaller程序打包工具