题目链接:

http://www.spoj.com/problems/GCJ1C09C/

题意:

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

2
8 1
3
20 3
3 6 14

Output

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.

思路:

dp。

实现:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXP = , MAXQ = , INF= 0x3f3f3f3f;
int n, p, q, a[MAXP + ], dp[MAXQ + ][MAXQ + ]; int solve()
{
memset(dp, , sizeof(dp));
a[] = ;
a[q + ] = p + ;
for (int j = ; j <= q + ; j++)
{
for (int i = ; i <= q + - j; i++)
{
dp[i][i + j] = INF;
for (int k = i + ; k < i + j; k++)
dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j]);
dp[i][i + j] += a[i + j] - a[i] - ;
}
}
return dp[][q + ];
} int main()
{
cin >> n;
for (int t = ; t <= n; t++)
{
cin >> p >> q;
for (int i = ; i <= q; i++)
{
scanf("%d", &a[i]);
}
cout << "Case #" << t << ": " << solve() << endl;
}
return ;
}

spoj GCJ1C09C Bribe the Prisoners的更多相关文章

  1. GCJ1C09C - Bribe the Prisoners

    GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...

  2. Bribe the Prisoners SPOJ - GCJ1C09C

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  3. 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. ...

  4. 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)

    一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...

  5. GCJ Round 1C 2009 Problem C. Bribe the Prisoners

    区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...

  6. spoj14846 Bribe the Prisoners

    看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...

  7. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  8. 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 ...

  9. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

随机推荐

  1. dict built-in function

    映射类型:字典 字典是无序的,映射类型对象里哈希值和被指向的对象是一对多的关系,字典中的键必须是可哈希的,所有不可变的类型都是可哈希的,另外针对数字键来说,值相等的两个数字是相同的键,例如1和1.0: ...

  2. jdk8新特性Stream

    Stream的方法描述与实例 1,filter  过滤 Person p1 = new Person(); p1.setName("P1"); p1.setAge(10); Per ...

  3. java 简单贪吃蛇

    1. [代码]java 简单程序     跳至 [1] [全屏预览]package com.snake;import java.awt.*;import javax.swing.*;import ja ...

  4. UVA-11078(水题)

    题意: 给一个序列,找两个整数a[i],a[j]使得a[i]-a[j]最大; 思路: 从前往后扫一遍;水题; AC代码: #include <bits/stdc++.h> /* #incl ...

  5. CodeForces161D: Distance in Tree(树分治)

    A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a ...

  6. 洛谷P3830 [SHOI2012]随机树——概率期望

    题目:https://www.luogu.org/problemnew/show/P3830 询问1:f[x]表示有x个叶节点的树的叶节点平均深度: 可以把被扩展的点的深度看做 f[x-1] ,于是两 ...

  7. JAVA Synchronized (三) volatile 与 synchronized 的比较

    一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...

  8. Vue HTML5 History 模式

    vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  9. 利用ASP .NET Core的静态文件原理实现远程访问Nlog日志内容及解决遇到的坑

    最近项目上试运行发现,很多时候网站出了问题或者某个功能不正常,常常需要运维人员去服务器里面查看一下日志,看看日志里面会产生什么异常,这样导致每次都要去远程服务器很不方便,有时服务器是客户保管的不能让我 ...

  10. 51nod1127【尺取】

    思路: 尺取,写挫了,debug了半天. #include <bits/stdc++.h> using namespace std; typedef long long LL; const ...