spoj14846 Bribe the Prisoners
看来我还是太菜了,这么一道破题做了那么长时间......
分析
我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够。于是我们考虑区间dp,dpij表示i到j区间的所有罪犯全部放走的最小花费,于是我们可以将一个区间(i,j)分为(i,k-1),(k+1,j)和k这个点,表示先取走点k的人,这样这个区间就被分成了两个,于是我们便可以转移的。详见代码。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int pl[],dp[][];
inline void init(){
memset(dp,0x7f,sizeof(dp));
}
int main(){
int n,m,i,j,k,t;
scanf("%d",&t);
for(int _=;_<=t;_++){
init();
scanf("%d%d",&m,&n);
for(i=;i<=n;i++)
scanf("%d",&pl[i]);
pl[n+]=m+;
for(i=;i<=n;i++)
dp[i][i]=pl[i+]-pl[i-]-;
for(i=;i<=n;i++)
for(j=;j+i-<=n;j++){
dp[j][j+i-]=min(dp[j+][j+i-],dp[j][j+i-]);
for(k=j+;k<j+i-;k++)
dp[j][j+i-]=min(dp[j][j+i-],dp[j][k-]+dp[k+][j+i-]);
dp[j][j+i-]+=pl[j+i]-pl[j-]-;
}
printf("Case #%d: ",_);
printf("%d\n",dp[][n]);
}
return ;
}
spoj14846 Bribe the Prisoners的更多相关文章
- GCJ1C09C - Bribe the Prisoners
GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...
- 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. ...
- spoj GCJ1C09C Bribe the Prisoners
题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to ...
- Bribe the Prisoners SPOJ - GCJ1C09C
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> ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- 囚徒问题(100 prisoners problem)的python验证
密码学课上老师介绍了这样一个问题,囚徒问题(100 prisoners problem):一百个囚徒被关在牢房里,典狱长给他们最后一次机会,100人依次进入一个有100个抽屉的牢房,每个抽屉置乱放入1 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
随机推荐
- vue mint ui 手册文档对于墙的恐惧
http://www.cnblogs.com/smallteeth/p/6901610.html npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm ...
- getParameter() getInputStream()和getReader() 区别
我们经常用servlet和jsp, 经常用request.getParameter() 来得到数据. request.getParameter() request.getInputStream() r ...
- CEF源码编译
CEF的构造说明:https://bitbucket.org/chromiumembedded/cef/wiki/BranchesAndBuilding chromium的源码地址:https://c ...
- MySQL 预处理语句prepare、execute、deallocate的使用
所以对于中文乱码,需要去check的地方有如下3个:1.mysql窗口的字符编码(xshell连接的远程工具的字符集设置):2.数据库的字符编码(show variables like '%char% ...
- testng监听ISuiteListener
实现ISuiteListener public class TestNgBeforeAction implements ISuiteListener{ public void onStart(ISui ...
- MyBatis 批量插入数据对插入记录数的限制
<基于 MyBatis 框架的批量数据插入的性能问题的探讨>(作者:魏静敏 刘欢杰 来源:<计算机光盘软件与应用> 2013 年第 19 期)中提到批量插入的记录数不能超过10 ...
- TCP和UDP数据包大小限制
1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...
- Python 正则表达式取值
import re class Retest: def __init__(self,string,path): self.string = string self.path = path def re ...
- 分析诊断工具之一:MYSQL性能查看(多指标)
网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...
- 使用Asset Pipeline管理rails生产环境静态资源实现步骤
1. 修改项目中指向静态资源文件的链接 a) 访问静态资源文件 <%= stylesheet_link_tag "application", media: &q ...