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 ;
}

Bribe the Prisoners SPOJ - GCJ1C09C的更多相关文章

  1. GCJ1C09C - Bribe the Prisoners

    GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...

  2. spoj GCJ1C09C Bribe the Prisoners

    题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to  ...

  3. 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. ...

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

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

  5. GCJ Round 1C 2009 Problem C. Bribe the Prisoners

    区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...

  6. spoj14846 Bribe the Prisoners

    看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...

  7. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  8. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  9. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

随机推荐

  1. 【Ehcache】基础知识学习

    一.Ehcache概述 1.1 简介 1.2 Ehcache的主要特性 二.Ehcache使用介绍 2.1 Ehcache缓存过期策略 2.2 如何解决缓存与db不同步的问题. 三.Ehcache 基 ...

  2. 美团集群调度系统HULK技术演进

    本文根据美团基础架构部/弹性策略团队负责人涂扬在2019 QCon(全球软件开发大会)上的演讲内容整理而成.本文涉及Kubernetes集群管理技术,美团相关的技术实践可参考此前发布的<美团点评 ...

  3. hadoop 通过distcp进行并行复制

    通过distcp进行并行复制 前面的HDFS访问模型都集中于单线程的访问.例如通过指定文件通配,我们可以对一部分文件进行处理,但是为了高效,对这些文件的并行处理需要新写一个程序.Hadoop有一个叫d ...

  4. Cookie的获取

    1.先创建Cookie对象,设置Cookie的键和值: Cookie cookie1="); Cookie cookie2="); Cookie cookie3="); ...

  5. 0x7fffffff的意思

    7fffffff是8位16进制 每个16进制代表4个bit 8✖4bit=32bit=4Byte f的二进制为:1111,7的二进制位0111 int类型的长度位4Byte 左边起,第一位为符号位,0 ...

  6. [Advanced Python] 11 - Implement a Class

    基础概念:[Python] 08 - Classes --> Objects 进阶概念:[Advanced Python] 11 - Implement a Class 参考资源:廖雪峰,面向对 ...

  7. JavaScript之对象Array

    数组Array是JavaScript中最常用的类型之一,同一数组中可以保存任意类型的数据,并且它的长度是动态的,会随着数组中数据的加减自动变化.每个数组都有一个表示其长度(数组元素的个数)的lengt ...

  8. numpy库使用总结

    numpy study 0x01:n维数组对象ndaarray 存放同类型元素的多维数组 0x02:numpy数据类型 numpy 的数值类型实际上是 dtype 对象的实例,并对应唯一的字符,包括 ...

  9. 使用file_get_contents() 发送GET、POST请求

    服务器端执行HTTP请求,大家经常使用的就是CURL,curl工具的确是很好的数据文件传输工具,那么除此之外还有其他的工具能实现这个功能吗? 现在为你介绍一个很常见的工具 file_get_conte ...

  10. Dubbo学习系列之十三(Mycat数据库代理)

    软件界有只猫,不用我说,各位看官肯定知道是哪只,那就是大名鼎鼎的Tomcat,现在又来了一只猫,据说是位东方萌妹子,暂且认作Tom猫的表妹,本来叫OpencloudDB,后又改名为Mycat,或许Ca ...