看来我还是太菜了,这么一道破题做了那么长时间......

传送门

分析

我首先想到的是用状压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的更多相关文章

  1. GCJ1C09C - Bribe the Prisoners

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

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

  3. spoj GCJ1C09C Bribe the Prisoners

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

  4. Bribe the Prisoners SPOJ - GCJ1C09C

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

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

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

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

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

  7. ProgrammingContestChallengeBook

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

  8. 囚徒问题(100 prisoners problem)的python验证

    密码学课上老师介绍了这样一个问题,囚徒问题(100 prisoners problem):一百个囚徒被关在牢房里,典狱长给他们最后一次机会,100人依次进入一个有100个抽屉的牢房,每个抽屉置乱放入1 ...

  9. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

随机推荐

  1. SpringCloud教程 | 第五篇: 路由网关(zuul)

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...

  2. bzoj 5403 Marshland

    $n \leq 50$ sol: 放一个在 $x$ 处拐弯的 $L$ 形石头相当于在水平和垂直方向上各选一个与 $x$ 相邻的点,全局不能重复选 最小化危险度,相当于满足这些限制的情况下石头盖住的点危 ...

  3. 重温CLR(一)CLR基础

    如果一个C#developer,对CLR没有了解,那就只能是入门级别.未来.NET CORE是趋势,但是.NET CORE 也是基于CoreCLR的,而CLR和CoreCLR其实差别不大,从runti ...

  4. ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...

  5. 前端用户输入校验--基于JQ

    <!DOCTYPE html> <html> <head> <title>用户输入校验</title> </head> < ...

  6. (C#)把磁盘目录树加载在窗体菜单中

    这又是一个没有技术含量的代码.写出来只是玩玩,所以也不敢放在首页. 这里有个问题,是获取文件/文件夹的图标.使用 System.Drawing.Icon.ExtractAssociatedIcon 只 ...

  7. Linux sed 批量替换多个文件中的字符串【转载】

    原文网址:http://blog.sina.com.cn/s/blog_730edb930100qzz5.html     比如,要将目录/modules下面所有文件中的zhangsan都修改成lis ...

  8. ajax中error函数参数详解

    xhr.status和error函数中的status是不一样的,error函数中的status主要包括:"success"."notmodified".&quo ...

  9. dirs命令

    dirs命令显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录).dirs始终显示当然目录, 再是堆栈中的内容:即使目录堆栈为空, dirs命令仍然只显示当然目录. 语法 dir ...

  10. Generate web.xml deployment descriptor

    eclipse 使用tomcat7.0建立Dynamic Web Project 时,next至步骤“Web Module”,此时勾选选项“Generate web.xml deployment de ...