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. 反序列化JSON

    本人编程生涯刚刚起步,以下是个人理解,如果有些不对的地方,请各位在评论区指出,如果有更详细的博客也可以推荐给我. 首先要根据JSON创建一个实体类,并且要实现Serializable接口,再创建一个J ...

  2. zabbix监控进程和端口存活脚本

    自定义脚本监控端口和进程,脚本process_port_check.sh 内容: [root@mysql02 data]# cat test.sh #!/bin/bash ############## ...

  3. Java web的基本概念

    概念一直是学习计算机软件开发中经常遇到的问题,也是软件行业最喜欢创造的东西.很多时候,学习计算机软件开发遇到困难都是因为对某些概念的不理解,而不是因为技术本身有多么复杂.Java Web作为Java ...

  4. java 中for循环中断的办法

    /* 中断for循环的办法: 1.break ***2.return是结束方法的,不是结束循环的. 3.标签的方法. 格式: 表签名:语句 运行结果:D:\test\day0413>java T ...

  5. 正确重写equals方法和compareTo方法

    一.概述 程序要对一堆数据元素排序,查找,增加删除.数据节点 class Node{ int type; int index; int score; } 规则: 1)对象相等:两个节点n1与n2,如果 ...

  6. Disruptor原理探讨

    之前谈到了在我的项目里用到了Disruptor,因为对它了解不足的原因,才会引发之前的问题,因此,今天特意来探讨其原理. 为什么采用Disruptor 先介绍一下我的这个服务.这个服务主要是作为游戏服 ...

  7. [VB.NET Tips]对多行文本的支持

    从Visual Studio 2008开始VB.NET支持多行文本. 用法如下: Dim mString As String = <string>我是 一个多 行文本.</strin ...

  8. spring web 脚手架 (持续更新中...)

    spring web 脚手架 项目地址: https://github.com/MengW9/scafflod.git 还有觉得哪些可以加上去的配置,欢迎各位拍砖,我会持续更新,大家共同进步 一个通用 ...

  9. Redis 集群(三)

    为什么为有集群 在 Redis3 版本之前,每台 Redis 机器需要存储所有 Redis key ,这要求每台 Redis 机器有足够大的内存 而且只能是主节点写,从节点读,对于高并发情况下会有性能 ...

  10. SpringCloudEureka入门

    说明 SpringBoot版本 2.1.7.RELEASE SpringCloud版本 Greenwich.SR2 创建eureka server工程 加入pom依赖 <dependencies ...