[Code Jam] Bribe the Prisoners
来源:互联网 发布:淘宝衣服女装夏装 编辑:程序博客网 时间:2024/05/21 21:50
Problem
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 Qdays, 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 Qwhere 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: Cwhere 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.
Small dataset
1 ≤ P ≤ 100
1 ≤ Q ≤ 5
Large dataset
1 ≤ P ≤ 10000
1 ≤ Q ≤ 100
Sample
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[i][j]代表将开区间(i, j)内的目标都释放完所需的金币。
于是dp[i][j] == min(dp[i][idx] + dp[idx][j])(i < idx < j)
+ v[j] - v[i] - 2 //释放idx的金币
这个题最重要的一个技巧是把dp[i][j]看成开区间,这样才可以很方便的转移。
又一个技巧是附加头尾,这样最终结果就是dp[0][q + 1]了。
#include<stdio.h>#define P 10010#define Q 110int cmp(const int *a, const int *b){return *a - *b;}int dp[Q][Q];int v[Q];int main(void){int t, tc;int i, j, k, idx;scanf("%d", &t);for(tc = 1; tc <= t; tc++){int p, q;scanf("%d%d", &p, &q);v[0] = 0;v[q + 1] = p + 1;for(i = 1; i <= q; i++){scanf("%d", &v[i]);}qsort(v, q + 1, sizeof(int), cmp);for(i = 0; i <= q; i++){dp[i][i + 1] = 0;}for(k = 2; k <= q + 1; k++){for(i = 0; i + k <= q + 1; i++){int min = 0x7fffffff;for(idx = i + 1; idx < i + k; idx++){int val = dp[i][idx] + dp[idx][i + k];if(val < min){min = val;}}dp[i][i + k] = min + v[i + k] - v[i] - 2; }}printf("Case #%d: %d\n", tc, dp[0][q + 1]);}return 0;}
- [Code Jam] Bribe the Prisoners
- Code Jam 2009 Round1C Problem C. Bribe the Prisoners —— 区间DP
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
- 记忆化搜搜——Code Jam 2009 Round 1C #C Bribe the Prisoners
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
- GCJ1C09C - Bribe the Prisoners
- Bribe the Prisoners(Easy)
- SPOJ - GCJ1C09C Bribe the Prisoners
- GCJ 2009 Bribe the Prisoners
- DP-记忆化dp--Bribe the Prisoners
- ***SPOJ - GCJ1C09C Bribe the Prisoners【贿赂囚犯】
- GCJ 2009 Round1C C Bribe the Prisoners
- 编程题-贿赂囚犯(Bribe the prisoners)-动态规划|剪枝
- 刷题: bribe the prisoners(2009 Round 1C C)
- GCJ 2009 Round 1C C (Bribe the Prisoners)
- GCJ--Bribe the Prisoners (2009 Round 1C C)
- GCJ 2009 Round 1C Bribe the Prisoners
- Code Jam--The Repeater
- ZOJ-1954
- hdu 1506 Largest Rectangle in a Histogram
- haproxy 配置 说明
- codeforces 426/A
- Java 5种字符串拼接方式性能比较。
- [Code Jam] Bribe the Prisoners
- android AsyncTask
- Codeforces Round #243 (Div. 2)——Sereja and Swaps
- C++ string_demo
- 揭秘SNS自主创业军团 军团高层月赚10万元不是问题
- MongoDB基本命令
- 阵列源码学习实践(一)阵列的开机自动检测
- spring中配置<aop:aspectj-autoproxy/>与<mvc:annotation-driven/>作用以及国际化配置
- AndroidPN环境建立