Google Code Jam 2009, Round 1C C. Bribe the Prisoners (記憶化dp)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   strong   資料   for   

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 Qdays, 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, NN 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.

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.

題目大意:

t 組測試資料,n個人在監獄,要放出m個人,每放出一個人,他周圍的人(兩邊連續的直到碰到空的監獄或者盡頭)都要賄賂1個錢,問最少的總花費

 

解題思路:

記憶化dp,dp(i,j) 表示從 編號 a[i] ~ a[j] 不包含 a[i] 與 a[j] 的子樹的花費

狀態轉移方程 d[i][j]=min(dp(i,k)+dp(k,j)+(a[j]-a[i]-2),d[i][j]) 

注意,一開始添加哨兵,0號位與n+1號位

 

 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);  出錯 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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.