动态规划(记忆化搜索)
来源:互联网 发布:新媒体排版软件 编辑:程序博客网 时间:2024/05/29 12:45
GCJ1C09C - Bribe the Prisoners
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 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: 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.
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
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.
一道并不是很难的题目
代码:
#define _CRT_SECURE_NO_DEPRECATE#include<iostream>#include<algorithm>#include<string.h>using namespace std;const int maxn = 100 + 5;#define INF 0x3f3f3f3fint d[maxn][maxn];int a[maxn];int dp(int i, int j){if (j - i <= 1)return 0;int &ans = d[i][j];if (ans >= 0)return ans;ans =INF;for (int k = i + 1; k < j; k++){ans = min(ans,dp(i, k)+dp(k, j)+ a[j] - a[i]-2);}return ans;}int main(){int n, p, q,iCase=1;scanf("%d", &n);while (n--){scanf("%d%d", &p, &q);a[0] = 0, a[q + 1] = p + 1;for (int i = 1; i <= q; i++) scanf("%d", &a[i]);memset(d, -1, sizeof(d));printf("Case #%d: %d\n",iCase++,dp(0, q + 1));}return 0;}
- 动态规划(记忆化搜索)
- 动态规划-记忆化搜索
- POJ 1088 滑雪(动态规划+记忆化搜索)
- poj 1088 滑雪(动态规划:记忆化搜索)
- uva 674 Coin Change (动态规划:记忆化搜索)
- uva 10651 Pebble Solitaire(动态规划:记忆化搜索)
- 矩形嵌套-记忆化搜索(dp动态规划)
- 硬币找零-记忆化搜索(DP动态规划)
- HDU 1579 记忆化搜索(递归+动态规划)
- poj 1390 Blocks(记忆化搜索/动态规划)
- poj 1191 棋盘分割(记忆化搜索/动态规划)
- poj 1088 滑雪(记忆化搜索/动态规划)
- (NYoj 10)skiing - 动态规划+记忆化搜索
- 记忆化搜索,动态规划(巴比伦塔,uva 437)
- 记忆化搜索,动态规划(旅行,uva 1347)
- 动态规划,记忆化搜索(分享巧克力,LA 4794)
- poj 1088 滑雪 动态规划(记忆化搜索)
- 记忆化搜索(DFS+动态规划)--滑雪
- opencv_vs上配置opencv
- Spring Boot 启动加载数据 CommandLineRunner
- HDU 4597 Play Game (博弈 + 区间dp)
- iOS总结
- Java 封装jdbc查询
- 动态规划(记忆化搜索)
- framework not found Bolts for architecture x86_64
- 指针的大小
- 不用除法求一个数组中除自己外的元素乘积
- 坚持#第21天~学好专业知识!
- Faster RCNN 添加 ROIPoolingLayer 和SmoothL1LossLayer
- activity详解一
- hdfs的一些命令
- 几种常见模式识别算法整理和总结