GCJ--Bribe the Prisoners (2009 Round 1C C)
来源:互联网 发布:java开发注册登陆 编辑:程序博客网 时间:2024/05/18 02:35
Problem
In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells numberi 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 tohis other neighbour, and so on until they reach a prisoner with no other neighbour (because he is in cell 1, or in cellP, 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 cellA, all prisoners housed on either side of cell A - until cell 1, cellP 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 ofQ 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 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 ≤ 100001 ≤ Q ≤ 100
以下是我的AC代码:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define MAX_Q 105using namespace std;int Q,P,A[MAX_Q];int dp[MAX_Q][MAX_Q];int T,temp;void solve(){A[0]=0;A[Q+1]=P+1;for(int q=0;q<=Q;q++){dp[q][q+1]=0;}for(int w=2;w<=Q+1;w++){for(int i=0;i+w<=Q+1;i++){int j=i+w,t=INT_MAX;for(int k=i+1;k<j;k++)t=min(t,dp[i][k]+dp[k][j]);dp[i][j]=t+A[j]-A[i]-2;}}printf("Case #%d: %d\n",temp-T,dp[0][Q+1]);}int main(){ freopen("C-large-practice.in","r",stdin); freopen("output.out","w",stdout); cin>>T; temp=T; while(T--) { scanf("%d%d",&P,&Q); for(int i=1;i<=Q;i++) scanf("%d",&A[i]); solve();}return 0; }
- GCJ 2009 Round 1C C (Bribe the Prisoners)
- GCJ--Bribe the Prisoners (2009 Round 1C C)
- GCJ 2009 Round 1C Bribe the Prisoners
- GCJ 2009 Round1C C Bribe the Prisoners
- Bribe the Prisoners(GCJ 2009 Round 1C C)(dp)
- 刷题: bribe the prisoners(2009 Round 1C C)
- GCJ 2009 Bribe the Prisoners
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
- 挑战2.7.3 Round 1C 2009 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)
- Bribe the Prisoners——GCJ 2009 Round1C C(区间dp)
- Code Jam 2009 Round1C Problem C. Bribe the Prisoners —— 区间DP
- GCJ1C09C - Bribe the Prisoners
- Bribe the Prisoners(Easy)
- SPOJ - GCJ1C09C Bribe the Prisoners
- [Code Jam] Bribe the Prisoners
- 2014 GCJ Round 1C
- Android 手势锁的实现 让自己的应用更加安全吧
- 线程池 转
- 求A*f(A,B,n-1)+B*f(A,B,n-2))%7;
- 8583报文学习
- 51nod 算法马拉松11 D 计算
- GCJ--Bribe the Prisoners (2009 Round 1C C)
- 做 LeetCode 有感
- androd 内存泄露分析
- 对Objective-C的runtime的一些理解
- iGrimaceV8 V8在线威锋源apt.so/qwkjv8手机直接下载安装教程
- Android 自定义ViewGroup手把手教你实现ArcMenu
- Linux Mint下的Mysql安装
- moodle 3.0 essential theme 主题 问题解决
- Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)