#P13439. [GCJ 2009 #1C] Bribe the Prisoners

    ID: 15313 远端评测题 2000~3000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>动态规划 DP2009区间 DPGoogle Code Jam

[GCJ 2009 #1C] Bribe the Prisoners

题目描述

In a kingdom there are prison cells (numbered 11 to PP) built to form a straight line segment. Cells number ii and i+1i+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 11, or in cell PP, 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 AA, all prisoners housed on either side of cell AA - until cell 11, cell PP 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 QQ prisoners to be released in QQ 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.

输入格式

The first line of input gives the number of cases, NN. NN test cases follow. Each case consists of 2 lines. The first line is formatted as

P QP \ Q

where PP is the number of prison cells and QQ is the number of prisoners to be released.

This will be followed by a line with QQ distinct cell numbers (of the prisoners to be released), space separated, sorted in ascending order.

输出格式

For each test case, output one line in the format

Case #XX: CC

where XX is the case number, starting from 11, and CC is the minimum number of gold coins needed as bribes.

2
8 1
3
20 3
3 6 14
Case #1: 7
Case #2: 35

提示

Sample Explanation

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=3519 + 12 + 4 = 35. If you instead release the person in cell 6 first, the cost will be 19+4+13=3619 + 4 + 13 = 36.

Limits

  • 1N1001 \leq N \leq 100
  • QPQ \leq P
  • Each cell number is between 11 and PP, inclusive.

Small dataset

  • Time limit: 20 2 seconds.
  • 1P1001 \leq P \leq 100
  • 1Q51 \leq Q \leq 5

Large dataset

  • Time limit: 30 3 seconds.
  • 1P100001 \leq P \leq 10000
  • 1Q1001 \leq Q \leq 100