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, ...
随机推荐
- amazon lightsail
https://51.ruyo.net/6038.html https://aws.amazon.com/cn/lightsail/
- lucene 5可以运行的demo
package hello; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.ap ...
- GDUT 积木积水 2*n 时间复杂度
题意 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元,于是他把这个三维的现实问 ...
- ubuntu安装wine+plsql
1.在ubuntu下装了win7的虚拟机,在使用plsql进行开发的时候发现很慢很卡,经常半天反应不过来.机器是不差的,1w5的thinkstation,实在受不了这种 速度,想着在ubuntu下搞一 ...
- I.MX6 eMMC分区挂载
/********************************************************************* * I.MX6 eMMC分区挂载 * 说明: * 如果想要 ...
- macbook pro上安装虚拟机
第一步:下载MacHunter的app应用商店 第二步:在MacHunter内下载Parallels Desktop虚拟机 第三步:如果在这个商店下载不下来,在网络资源上直接下载Parallels D ...
- JAVA编程思想中总结的与C++的区别
Java和C++都是面向对象语言.也就是说,它们都能够实现面向对象思想(封装,继乘,多态).而由于c++为了照顾大量的C语言使用者,而兼容了C,使得自身仅仅成为了带类的C语言,多多少少影响了其面向对象 ...
- BZOJ3895 rock
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3895 看这题感觉好神. SG函数,dp....好像都不行呀. 最后去膜拜题解发现记忆化搜索 囧 ...
- Windows可以ping通百度,但是用浏览器打不开网页
开始——>运行——>输入cmd回车——>输入: netsh winsock reset 命令(重置winsock文件)——>重启系统. 重启完系统,即可解决:如不能,请再查找 ...
- C#(KeyChar和KeyCord值,KeyDown/KeyPress事件区别)
1. 首先将窗口属性KeyPreview设为true,如果属性对话框中找不到,就直接在代码里添加:2. 添加KeyPress / KeyDown事件: KeyPress 和KeyDown .KeyPr ...