HDU 3237 Help Bubu

DP。。。。。

Help Bubu

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1096    Accepted Submission(s): 316


Problem Description
Bubu's bookshelf is in a mess! Help him!

There are  n books on his bookshelf. We define the mess value to be the number of segments of  consecutive equal-height books. For example, if the book heights are 30, 30, 31, 31, 32, the mess value is 3, that of 30, 32, 32, 31 is also 3, but the mess value of 31, 32, 31, 32, 31 is 5 - it's indeed in a mess!

Bubu wants to reduce the mess value as much as possible, but he's a little bit tired, so he decided to take out at most  k books, then put them back somewhere in the shelf. Could you help him?
 

Input
There will be at most 20 test cases. Each case begins with two positive integers  n and  k (1 <=  k <=  n <= 100), the total number of books, and the maximum number of books to take out. The next line contains  n integers, the heights of each book, from left to right. Each height is an integer between 25 and 32, inclusive. The last test case is followed by  n =  k = 0, which should not be processed.
 

Output
For each test case, print the case number and the minimal final mess value. Print a blank line after the output of each test case.
 

Sample Input
   
   
   
   
5 2 25 25 32 32 25 5 1 25 26 25 26 25 0 0
 

Sample Output
   
   
   
   
Case 1: 2 Case 2: 3
 

Source
2009 Asia Wuhan Regional Contest Hosted by Wuhan University
 


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl
const int N = 105;
const int inf  = 0x3f3f3f3f;
int dp[2][265][N][10]; ///pos, mask, k books, last
int n, lim;
int a[N];
inline void Min(int &a, int b) { a=min(a,b); }
int num[324];
int main()
{
    int ca = 1;
    num[0]=0;
    for(int i=1;i<256;i++) num[i]=num[i>>1]+(i&1);
    while(cin>>n>>lim, n)
    {
        int cnt[N]; memset(cnt,0,sizeof cnt);
        for(int i=0;i<n;i++) cin>>a[i],a[i]-=25;
        memset(dp, 63, sizeof dp);
        int total = 0;
        for(int i=0;i<n;i++) total |= (1<<a[i]);
        dp[0][0][0][8] = 0;
        int now = 0;
        for(int i=0;i<n;i++)
        {
            for(int mask=0;mask<(1<<8);mask++) ///每种书当前有没有全部拿出来
            {
                for(int k=0;k<=lim&&k<=i;k++)  ///take books----已经拿了多少书
                {
                    for(int j=0;j<=8;j++)   ///last book --- 序列中上一本书高度
                    {
                        int t=dp[now][mask][k][j];
                        if(t>=inf) continue;
                       /// printf("dp[%d][%d][%d] = %d\n", mask, k, j, t);
                        ///不把书拿出来
                        int add=0;
                        if(j==a[i]) add=0;
                        else add=1;
                        Min(dp[1-now][mask|(1<<a[i])][k][a[i]], t+add); ///这本书留下来,有了这种书

                        ///take i book  把书拿出来
                        if(k==lim) continue;
                        Min(dp[1-now][mask][k+1][j], t);
                    }
                }
            }
            memset(dp[now], 63, sizeof dp[now]);
            now = 1 - now;
        }
        int ans=inf;
        for(int s=0;s<256;s++)
        {
            for(int k=0;k<=lim;k++)
            {
                for(int j=0;j<=8;j++)
                {
                    int t=dp[now][s][k][j];
                    if(t>=inf) continue;
                    ans=min(ans, dp[now][s][k][j] + num[total^s]);
                   /// printf("dp[%d][%d][%d] = %d\n", s, k, j, dp[now][s][k][j]);
                }
            }
        }
        printf("Case %d: %d\n\n", ca++, ans);
    }
    return 0;
}
/**
5 2
25 25 32 32 25
5 1
25 26 25 26 25
5 1
25 30 25 30 25
5 2
25 30 25 30 25
5 5
25 30 25 30 25
0 0
*/


你可能感兴趣的:(HDU 3237 Help Bubu)