Bribe the Prisoners SPOJ - GCJ1C09C
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, 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 |
Output |
2 |
Case #1: 7 |
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的更多相关文章
- GCJ1C09C - Bribe the Prisoners
GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...
- spoj GCJ1C09C Bribe the Prisoners
题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to ...
- 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. ...
- 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)
一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...
- GCJ Round 1C 2009 Problem C. Bribe the Prisoners
区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...
- spoj14846 Bribe the Prisoners
看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
随机推荐
- 实现一个基于码云的Storage
实现一个简单的基于码云(Gitee) 的 Storage Intro 上次在 asp.net core 从单机到集群 一文中提到存储还不支持分布式,并立了一个 flag 基于 github 或者 开源 ...
- mysql 查询参数尾部有空格时被忽略
最近再使用mysql时,无意见发现,当我查询参数尾部输入若干个空格时,依然可以查出和不加空格相同的结果,像这样 select * from wa where name='be ' 等同于 select ...
- 每天学会一点点(HashMap实现原理及源码分析)
HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希 ...
- mapper文件中“添加一条新数据并返回此数据的ID(主键)”的方法
在mapper文件的insert语句前加上<selectKey>标签即可 如下: 添加前测试: 添加后测试:
- javase复习(一)
break,continue,return区别: continue:跳出本次循环,还要再执行下次循环 break:跳出循环,若有多层循环则只跳出本层循环,其他层的循环需要挨个break return: ...
- Maven学习归纳(二)——几个常用命令解析
Maven的常用命令 第一次执行命令的时候,因为需要下载执行命令的基础环境,所以会从远程仓库下载该环境到本地仓库中 运行mvn命令,必须在pom.xml文件所在的目录 一. JavaProject的p ...
- Python3实战Spark大数据分析及调度 (网盘分享)
Python3实战Spark大数据分析及调度 搜索QQ号直接加群获取其它学习资料:715301384 部分课程截图: 链接:https://pan.baidu.com/s/12VDmdhN4hr7yp ...
- Dart语言概览
## Dart特性 Dart同时支持JIT(Just In Time,即时编译)和AOT(Ahead of Time,运行前编译)两种编译模式. **JIT** 在运行时即时编译,在开发周期中使用,可 ...
- request对象的方法
request对象封装的是请求的数据,由服务器创建,作为实参传递给Servlet的方法,一个请求对应一个request对象,request对象可以获得请求数据. 1.获取请求行信息 (1)get提交 ...
- VMware 虚拟机三种网络模式详解
一.前言 Vmware 为我们提供了三种网络工作模式,分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-only(仅主机模式). 二.VMware 的几个常见虚拟设备 打开 V ...