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 ...
随机推荐
- java 随机流
Example10_8.java import java.io.*; public class Example10_8 { public static void main(String args[]) ...
- elasticsearch高级配置之(二)----线程池设置
elasticsearch 配置 线程池 一个Elasticsearch节点会有多个线程池,但重要的是下面四个: 索引(index):主要是索引数据和删除数据操作(默认是cached类型) 搜索 ...
- php csv操作
csv的写入数据: $data = array( array('qq号','登录时间','名称'), array('123456','2012-08-21 15:21:10'.chr(1),'我是来测 ...
- 使用MyBatis的Generator自动创建实体类和dao的接口与xml
在实际的项目中其实建立数据库和设计数据库的时候特别重要,而等数据库设计完成之后,根据数据库创建实体类的工作就特别麻烦和繁琐了,不仅很麻烦,而且很浪费时间,不做又不行,这次就找到了一个简单的方法可以让m ...
- Django: 配置和静态文件
运行django-admin.py startproject [project-name] 命令会生成一系列文件,在django 1.6版本以后的settings.py文件中有以下语句: # Buil ...
- jq的合成事件
jq中有两个合成事件 hover()和toggle() 1.hover() hover方法用于模拟光标悬停事件.当光标移动到元素上时,会触发指定的第一个函数(enter),当光标移出这个元素时,会触发 ...
- MySQL架构优化:定时计划任务与表分区
转自: MySQL架构优化实战系列3:定时计划任务与表分区 - 今日头条(TouTiao.com)http://toutiao.com/a6304736482361049345/?tt_from=mo ...
- spring-jms
http://haohaoxuexi.iteye.com/blog/1893038 理解PooledConnectionFactory.CachingConnectionFactory和SingleC ...
- C#入门经典第七章,错误调试
调试模式下执行应用程序-------F5或是绿色的运行箭头 非模式下,调试---开始执行不调试(ctrl+F5)
- Exponential notation
Exponential notation You are given a positive decimal number x. Your task is to convert it to the &q ...