GCJ1C09C - Bribe the Prisoners
2016-04-20 20:54
381 查看
GCJ1C09C - Bribe the Prisoners
no tags 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 | Output |
2 8 1 3 20 3 3 6 14 | Case #1: 7 Case #2: 35 |
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
dp[l][r]表示第l间监狱到第r间监狱最少贿赂金币数。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f;
const int N=150;
int p,q,a
;
int dp
;
int gold(int l,int r)
{
return r-l-2;
}
int solve(int l,int r)//a[l]a[r]是空洞
{
if(dp[l][r]!=INF) return dp[l][r];
int num=gold(a[l],a[r]);
for(int i=l+1;i<r;i++)
{
int sum=num;
sum+=solve(l,i);
sum+=solve(i,r);
dp[l][r]=min(dp[l][r],sum);
}
if(dp[l][r]==INF)
dp[l][r]=0;
return dp[l][r];
}
int main()
{
int cas=0;
freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&p,&q);
for(int i=0;i<N;i++)
fill(dp[i],dp[i]+N,INF);
for(int i=1;i<=q;i++)
scanf("%d",&a[i]);
a[0]=0;
a[q+1]=p+1;
printf("Case #%d: %d\n",++cas,solve(0,q+1));
}
return 0;
}
相关文章推荐
- Spymemcached操作队列分析
- CentOS 7, lamp
- jquery源码学习1
- windos基本命令 查看指定端口,将指定进程杀死
- 读写存储文件中的数据
- android下dev设备的权限问题
- 【POJ 1743】Musical Theme
- SharePoint 第一个网站
- 不圆
- Asp.net Mvc Action重定向总结
- HDU 2509 Be the Winner
- java中方法重载与重写的区别
- Linux常用命令(持续更新)
- 每日工作总结02
- Netty中ChannelHandler共享数据的方式
- Metal 着色语言编程指南 九
- 弹窗
- ssh 利用ajaxfileupload实现图片无刷新上传
- twisted下使用网络连接池
- 高德地图 js api 使用