区间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的更多相关文章

  1. Google Code Jam Round 1C 2015 Problem A. Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

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

  3. TCO 2014 Round 1C 概率DP

    TCO round 1C的 250 和500 的题目都太脑残了,不说了. TCO round 1C 950 一个棋子,每次等概率的向左向右移动,然后走n步之后,期望cover的区域大小?求cover, ...

  4. GCJ1C09C - Bribe the Prisoners

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

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

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

  7. GCJ——Crazy Rows (2009 Round 2 A)

    题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...

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

  9. Kickstart Round D 2017 problem A sightseeing 一道DP

    这是现场完整做出来的唯一一道题Orz..而且还调了很久的bug.还是太弱了. Problem When you travel, you like to spend time sightseeing i ...

随机推荐

  1. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  2. 后台如何解析json

    1.以前倒是没有怎没遇到过需要后台解析的,由于json传到后台实际上是传的一个字符串通常用到批量删除时 var rows = $('#tt').datagrid('getSelections'); v ...

  3. IOS 使用动态库(dylib)和动态加载framework

    在iphone上使用动态库的多为dylib文件,这些文件使用标准的dlopen方式来使用是可以的.那相同的在使用framework文件也可以当做动态库的方式来动态加载,这样就可以比较自由的使用appl ...

  4. python 基本的序列和映射规则

    >>> def checkIndex(key):...     if not isinstance(key,(int,long)):raise TypeError...     if ...

  5. JS中获取页面单选框radio和复选框checkbox中当前选中的值

    单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...

  6. 深入理解.net多线程(一)

    多线程开发要理解的几个基本概念:进程.应用程序域.对象上下文 进程:进程是一个操作系统级别的概念,用来描述一组资源和程序运行所必需的内存分配.简单的理解,可以认为进程就是一个运行程序.对于每一个被加载 ...

  7. drawable文件夹详解

    QVGA使用ldpi,虽然有不同尺寸,但都是120dpi左右:HVGA同理:如下图: -finger    用于触摸屏的设备 -hdpi    近似于240dpi的高级显示密度的屏幕 -mdpi    ...

  8. ubuntu 解压 windows 生成的 zip 文件乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 有两种方式解决问题:(建议采用方法 ...

  9. 增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 -- 浅析android事件过滤策略

    http://blog.csdn.net/silenceburn/article/details/6083375 =========================================== ...

  10. Not a million dollars ——a certain kind of ingenuity, discipline, and proactivity that most people seem to lack

    原文:http://ryanwaggoner.com/2010/12/you-dont-really-want-a-million-dollars/a certain kind of ingenuit ...