Bribe the Prisoners SPOJ - GCJ1C09C
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 hisother 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 |
Case #1: 7 |
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水题,套路状态dp[l][r]表示将l到r的点全部搞完的最小代价,因为这个状态转移会重复所以考虑加一个限制条件,不包括两个端点。
那么我们就套路枚举断点,暴力转移,dp[l][r]=min(dp[l][k]+dp[k][r]+w[r]-w[l]-2)减去2是因为不算端点,要加两个关键点0和n,答案就是dp[0][n](因为不考虑端点),我是写的记忆化搜索,自然一点,如果for的话先枚举一个len就可以了。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define ll long long
#define MAXN 500
using namespace std;
int n,q,a[MAXN],w[MAXN],b[MAXN][MAXN];
ll dp[MAXN][MAXN]; ll dfs(int l,int r){
if(b[l][r]) return dp[l][r];
if(l+==r) return ;
b[l][r]=;
ll tmp=<<;
for(int i=l+;i<=r;i++){
tmp=min(tmp,dfs(l,i)+dfs(i,r)+w[r]-w[l]-);
}
dp[l][r]=tmp;
return tmp;
} int main()
{
int t;cin>>t;int Case=;
while(t--){
scanf("%d%d",&n,&q);
memset(b,,sizeof(b));
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(w,,sizeof(w));
for(int i=;i<=q;i++) scanf("%d",&a[i]);
a[++q]=,a[++q]=n+;
sort(a+,a+q+);
for(int i=;i<=q;i++) w[i]=a[i];
int k=unique(w+,w+q+)-w-;
for(int i=;i<=q;i++) a[i]=lower_bound(w+,w+k+,a[i])-w;
printf("Case #%d: %lld\n",++Case,dfs(,k));
}
return ;
}
Bribe the Prisoners SPOJ - GCJ1C09C的更多相关文章
- GCJ1C09C - Bribe the Prisoners
GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...
- spoj GCJ1C09C Bribe the Prisoners
题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to ...
- 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, ...
随机推荐
- 【LeetCode】763-划分字母区间
title: 763-划分字母区间 date: 2019-04-15 21:10:46 categories: LeetCode tags: 字符串 贪心思想 双指针 题目描述 字符串 S 由小写字母 ...
- java路障CyclicBarrier
当所有线程都执行到某行代码,才可已往下执行: package threadLock; import java.util.Random; import java.util.concurrent.Brok ...
- 二进制协议gob及msgpack介绍
本文主要介绍二进制协议gob及msgpack的基本使用. 最近在写一个gin框架的session服务时遇到了一个问题,Go语言中的json包在序列化空接口存放的数字类型(整型.浮点型等)都序列化成fl ...
- Git使用教程 Windows使用Git全攻略
在Windows上安装Git 我们需要先把Git的安装包下载下来,访问网址:https://git-for-windows.github.io/ 或者百度网盘:链接: https://pan.baid ...
- gallery的简单使用方法
Gallery意思是"画廊",就是一个水平可左右拖动的列表,里面可以放置多个图片,经常和ImageSwitcher一起使用 在使用ImageSwitcher时,需要传入一个View ...
- idea控制台乱码解决方案
第一步:修改intellij idea配置文件: 找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别 ...
- C# DATETIME格式转换汇总 根据日期过期星期
C# DateTime.Now.Year --2019(年) DateTime.Now.Month --9(月) DateTime.Now.Day --19(日) DateTime.Now.Hou ...
- Jenkins把GitHub项目做成Docker镜像
本文是<Jenkins流水线(pipeline)实战>系列的第三篇,前面已对Jenkins流水线有了基本认识,也试过从GitHub下载pipeline脚本并执行,今天的实战是编写一段pip ...
- 44 (OC)* 野指针、空指针
一: 空指针 : 没有存储任何内存地址的指针就称为空指针(NULL指针). 被赋值为nil的指针,在没有被具体初始化之前,为nil.nil.Nil.NULL.NSNULL的含义和区别 nil:OC中的 ...
- [CUDA] 00 - GPU Driver Installation & Concurrency Programming
前言 对,这是一个高大上的技术,终于要做老崔当年做过的事情了,生活很传奇. 一.主流 GPU 编程接口 1. CUDA 是英伟达公司推出的,专门针对 N 卡进行 GPU 编程的接口.文档资料很齐全,几 ...