Google Code jam 2009, round 1C C. bribe the prisoners (memorizing DP)

Source: Internet
Author: User

Problem

In a kingdom there are prison cells (numbered 1P) Built to form a straight line segment. cells numberIAndI + 1Are adjacent, and prisoners in adjacent cells are called "neighbors." A wall with a window separates adjacent cells, and neighbors can communicate through that window.

All prisoners live in peace until a prisoner is released. When that happens, the released prisoner's neighbors find out, and each communicates this to his other neighbor. That prisoner passes it onHisOther neighbor, and so on until they reach a prisoner with no other neighbor (because he is in cell 1, or in CellP, 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 CellA, All prisoners housed on either side of CellA-Until cell 1, cellPOr 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 listQPrisoners to be released inQDays, 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.NTest Cases Follow. Each case consists of 2 lines. The first line is formatted

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
QP
Each cell number is between 1 andP, Intrusive.

Small Dataset

1 ≤P≤ 100
1 ≤Q≤ 5

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.

Question:

T group test data: N people are in jail, and m people are to be released. Every time a person is released, the people around him (the two sides are continuous until they encounter an empty prison or an end) one bribe is required, and the minimum total cost is required.

 

Solution:

Memory DP, dp (I, j) indicates from number A [I] ~ The cost of a [J] subtree that does not contain a [I] And a [J]

State transition equation d [I] [J] = min (dp (I, K) + dp (K, j) + (A [J]-A [I]-2 ), d [I] [J])

Note: add the number 0 and number n + 1 as the sentry.

 

 1 void solve() 2 { 3     a[0]=0;a[m+1]=n+1; 4     for(int i=0;i<=n;i++) 5         dp[i][i+1]=0; 6     for(int w=2;i+w<=m+1;i++) 7     { 8         for(int i=0;i+w<=m+1;i++) 9         {10             int j=i+w,t=inf;11             for(int k=i+1;k<j;k++)12                 t=min(t,dp[i][k]+dp[k][j]);13             dp[i][j]=t+a[j]-a[i]-2;14         }15     }16     cout<<dp[0][m+1]<<endl;17 }

 

1 # include "iostream" 2 # include "cstdio" 3 # include "cstring" 4 # include "algorithm" 5 using namespace STD; 6 const int INF = 1e9; 7 const int MS = 110; 8 int A [MS], DP [MS] [MS], n, m, p; 9 int solve (int I, Int J) 10 {11 if (DP [I] [J]! = Inf) 12 Return DP [I] [J]; 13 if (I + 1> = J) 14 return 0; 15 for (int K = I + 1; k <j; k ++) 16 DP [I] [J] = min (solve (I, K) + solve (K, J) + (A [J]-A [I]-2), DP [I] [J]); 17 return DP [I] [J]; 18} 19 int main () 20 {21 int t; 22 p = 1; 23 CIN> T; 24 while (t --) 25 {26 CIN> N> m; 27 A [0] = 0; A [M + 1] = n + 1; 28 for (INT I = 1; I <= m; I ++) 29 CIN> A [I]; 30 // fill (DP, DP + sizeof (DP)/sizeof (INT), INF ); error 31 for (INT I = 0; I <= m + 1; I ++) 32 for (Int J = 0; j <= m + 1; j ++) 33 DP [I] [J] = inf; 34 int ans = solve (0, m + 1 ); 35 cout <"case #" <p ++ <":" <ans <Endl; 36} 37 return 0; 38}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.