spoj GCJ1C09C Bribe the Prisoners
题目链接:
http://www.spoj.com/problems/GCJ1C09C/
题意:
In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall with a window separates adjacent cells, and neighbours can communicate through that window.
All prisoners live in peace until a prisoner is released. When that happens, the released prisoner's neighbours find out, and each communicates this to his other neighbour. That prisoner passes it on to his other neighbour, and so on until they reach a prisoner with no other neighbour (because he is in cell 1, or in cell P, or the other adjacent cell is empty). A prisoner who discovers that another prisoner has been released will angrily break everything in his cell, unless he is bribed with a gold coin. So, after releasing a prisoner in cell A, all prisoners housed on either side of cell A - until cell 1, cell P or an empty cell - need to be bribed.
Assume that each prison cell is initially occupied by exactly one prisoner, and that only one prisoner can be released per day. Given the list of Q prisoners to be released in Q days, find the minimum total number of gold coins needed as bribes if the prisoners may be released in any order.
Note that each bribe only has an effect for one day. If a prisoner who was bribed yesterday hears about another released prisoner today, then he needs to be bribed again.
Input
The first line of input gives the number of cases, N. N test cases follow. Each case consists of 2 lines. The first line is formatted as
P Q
where P is the number of prison cells and Q is the number of prisoners to be released.
This will be followed by a line with Q distinct cell numbers (of the prisoners to be released), space separated, sorted in ascending order.
Output
For each test case, output one line in the format
Case #X: C
where X is the case number, starting from 1, and C is the minimum number of gold coins needed as bribes.
Limits
1 ≤ N ≤ 100
Q ≤ P
Each cell number is between 1 and P, inclusive.
Large dataset
1 ≤ P ≤ 10000
1 ≤ Q ≤ 100
Sample
Input
2
8 1
3
20 3
3 6 14
Output
Case #1: 7
Case #2: 35
Note
In the second sample case, you first release the person in cell 14, then cell 6, then cell 3. The number of gold coins needed is 19 + 12 + 4 = 35. If you instead release the person in cell 6 first, the cost will be 19 + 4 + 13 = 36.
思路:
dp。
实现:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXP = , MAXQ = , INF= 0x3f3f3f3f;
int n, p, q, a[MAXP + ], dp[MAXQ + ][MAXQ + ]; int solve()
{
memset(dp, , sizeof(dp));
a[] = ;
a[q + ] = p + ;
for (int j = ; j <= q + ; j++)
{
for (int i = ; i <= q + - j; i++)
{
dp[i][i + j] = INF;
for (int k = i + ; k < i + j; k++)
dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j]);
dp[i][i + j] += a[i + j] - a[i] - ;
}
}
return dp[][q + ];
} int main()
{
cin >> n;
for (int t = ; t <= n; t++)
{
cin >> p >> q;
for (int i = ; i <= q; i++)
{
scanf("%d", &a[i]);
}
cout << "Case #" << t << ": " << solve() << endl;
}
return ;
}
spoj GCJ1C09C 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 ...
- Bribe the Prisoners SPOJ - GCJ1C09C
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- 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. ...
- 贿赂囚犯 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> ...
- spoj14846 Bribe the Prisoners
看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
随机推荐
- Hive两种访问方式:HiveServer2 和 Hive Client
老版HiveClient: 要求比较多,需要Hive和Hadoop的jar包,各配置环境. HiveServer2: 使得与YARN和HDFS的连接从Client中独立出来, ...
- Hadoop MapReduce两种架构 以及 YARN
一.MRv1 Master - Slave 模式 存在JobTracker单点失败的问题,在YARN得到了解决. 主要包含4部分:JobTracker,TaskTracker,Task,Client ...
- DedeCms如何调用Discuz论坛主题等数据方法总结
DedeCms如何调用Discuz论坛主题等数据方法总结 同时使用Dedecms和Discuz论坛的朋友,难免要在网站内调用论坛的内容.使用Discuz论坛的JS调用方式,对搜索引擎不够友好,下面我们 ...
- 【TJOI2013】 单词
[题目链接] 点击打开链接 [算法] AC自动机+递推 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 200 ...
- gunicorn启动django时静态文件的加载
目前在用nginx+gunicorn对django进行部署 当我用gunicorn -w 4 -b 127.0.0.1:8080 myproject.wsig:application启动django时 ...
- node.js适合游戏后台开发吗?
网站服务器和游戏服务器是怎么样联系到一起的? 百牛信息技术bainiu.ltd整理发布于博客园 1. 游戏分很多种,咱们先来看看MMORPG. 再怎么简单的RPG服务器都免不了处理多人交互的情形,上百 ...
- Crontab Build_setting的定期检查
一.脚本功能 (1)检查所有的builting_setting.h是否能够编译通过,并将编译结果写入 编译结果.h文件中. (2)将编译结果通过邮箱发送给相关负责人. (3)系统定期执行任务,检查bu ...
- 【旧文章搬运】再谈隐藏进程中的DLL模块
原文发表于百度空间,2009-09-17========================================================================== 相当老的话 ...
- oracle 在insert into的时候报ORA-00928: missing SELECT keyword错 [问题点数:100分,结帖人dm520]
转自:https://bbs.csdn.net/topics/310095274 INSERT INTO SA_Table(uniPositionCode,transferGroupName,appC ...
- C++中的new用法总结
前段时间复习面试的时候,看到这个问题经常有问到,我这个小白就看了些博客和书,总结一下. new可以说是个一个关键字,也可以说是一个运算符,并且可以被重载. 1.new operator 这个就是平时最 ...