GCJ Round 1C 2009 Problem C. Bribe the Prisoners
区间DP。dp[i][j]表示第i到第j个全部释放最小费用。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;
int T,P,Q;
int a[maxn];
int dp[maxn][maxn]; void read()
{
scanf("%d%d",&P,&Q);
for(int i=;i<=Q;i++) scanf("%d",&a[i]);
sort(a+,a++Q);
a[]=; a[Q+]=P+;
} void init()
{
memset(dp,,sizeof dp);
for(int i=;i<=Q;i++) for(int j=i;j<=Q;j++) dp[i][j]=INF;
} void work()
{
for(int i=;i<=Q;i++)
{
for(int j=;j<=Q;j++)
{
int st=j,en=st+i-;
if(en>Q) continue;
if(i==)
{
dp[st][en]=a[st]-a[st-]-+a[st+]-a[st]-;
continue;
} for(int k=st;k<=en;k++)
{
dp[st][en]=min(
dp[st][en],
dp[st][k-]+dp[k+][en]+(a[en+]--a[st-])-
);
}
}
}
printf("%d\n",dp[][Q]);
} int main()
{
// freopen("D:\\test.in","r",stdin);
// freopen("D:\\test.out","w",stdout);
scanf("%d",&T);
for(int Case=;Case<=T;Case++)
{
read();
init();
printf("Case #%d: ",Case);
work();
}
return ;
}
GCJ Round 1C 2009 Problem C. Bribe the Prisoners的更多相关文章
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
- TCO 2014 Round 1C 概率DP
TCO round 1C的 250 和500 的题目都太脑残了,不说了. TCO round 1C 950 一个棋子,每次等概率的向左向右移动,然后走n步之后,期望cover的区域大小?求cover, ...
- 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. ...
- Google Code Jam 2010 Round 1C Problem B. Load Testing
https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...
- GCJ——Crazy Rows (2009 Round 2 A)
题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...
- Google Code Jam Round 1A 2015 Problem B. Haircut 二分
Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...
- Kickstart Round D 2017 problem A sightseeing 一道DP
这是现场完整做出来的唯一一道题Orz..而且还调了很久的bug.还是太弱了. Problem When you travel, you like to spend time sightseeing i ...
随机推荐
- dump文件生成与调试(VS2008)
总结一下dump文件生成和调试的方法: 1:用SetUnhandledExceptionFilter捕获未处理的异常,包含头文件<windows.h>.函数原型为: LPTOP_LEVEL ...
- linux通用邻居基础架构
1.为每一个协议提供一个缓存来存放L3到L2的转换结果. 2.提供在缓存中添加.删除.改变和查找一个特定映射项的函数.查找函数必须要快,因为它会影响整个系统的性能. 3.为每一个协议缓存的数据项提供一 ...
- 最短路径算法——Dijkstra算法
在路由选择算法中都要用到求最短路径算法.最出名的求最短路径算法有两个,即Bellman-Ford算法和Dijkstra算法.这两种算法的思路不同,但得出的结果是相同的. 下面只介绍Dijkstra算法 ...
- 借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器。
rz 是将window文件传到linux服务器上,到执行rz命令的目录 sz 可以将linux文件发送到windows上,可以选择目录. https://www.google.com/ncr 登录一下 ...
- 分布式数据库Cobar
Cobar简介: Cobar是关系型数据库的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务. 产品在阿里巴巴B2B公司已经稳定运行了3年以上. 目前已经接管了3000 ...
- hibernate事务并发问题(脏读,不可重复读,幻读)
脏读 dirty read: 读了别的事务没有提交的事务, 可能回滚, 数据可能不对. 不可重复读 non repeatable read: 同一个事务里前后读出来的数据不一样, 被另一个事务影响 ...
- 虚拟ip
网卡上增加一个IP: ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0 删除网卡的第二个IP地址: ip addr del 192.168.0.1 d ...
- java dom4j解析xml实例(3)
代码运行前需要先导入dom4j架包. 需要解析的XML文件test.xml如下: <students> <student age="25"><!--如 ...
- android脚步---不同界面之间切换
对于一个app,可能需要多个界面,使用Button或者其他控件在不同界面之间切换,那么如何做到呢 首先必须明确,一般一个activity.java文件一般只对应一个界面即一个layout.xml文件, ...
- VS找不到MFC90d.dll错误
VS 2005/VS 2008在生成可执行文件时使用了一种新的技术,该技术生成的可执行文件会伴随生成一个清单文件(manifest file)(.manifest后缀文件)(其本质上是XML文档,你可 ...