Help Bubu - UVa 12235 四维状压dp

来源:互联网 发布:c语言算闰年 编辑:程序博客网 时间:2024/06/04 21:43

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$ \le$k$ \le$n$ \le$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

题意:给你n本的高度,你最多可以改变其中k本的位置,连续的高度相同的一堆书的杂乱度为1,问最后杂乱度的最低值是多少。
思路:可以假设每次改变一本书的时候就是把它拿出来,然后最后再统一放入,这样的话如果剩下的书有那种高度的,就放入那本书的旁边,否则的话,就只能增加一个杂乱度了。dp[i][j][S][k]表示当前处理到第i本书,已经改变了其中k本书的位置,并且之前剩下的书的状压状态为S,最后一本书的高度为k的时候的最小的杂乱度。
AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int dp[2][102][256][9],n,m,val[110],vis[10],num[260];int solve(int S){    int ret=0;    while(S)    {        if(S&1)          ret++;        S/=2;    }    return ret;}int main(){    int t=0,i,j,k,a,b,temp,S,S2,len,ret,ans;    for(i=0;i<=256;i++)       num[i]=solve(i);    while(~scanf("%d%d",&n,&m) && n+m>0)    {        memset(vis,0,sizeof(vis));        for(i=1;i<=n;i++)        {            scanf("%d",&val[i]);            val[i]-=24;            vis[val[i]]++;        }        ret=0;        for(i=1;i<=8;i++)           if(vis[i]>0)             ret++;        memset(dp,INF,sizeof(dp));        dp[0][0][0][0]=0;        len=1<<8;        for(i=1;i<=n;i++)        {            if(i&1)              a=0,b=1;            else              a=1,b=0;            memset(dp[b],INF,sizeof(dp[b]));            for(j=0;j<=min(i,m);j++)            {                for(S=0;S<len;S++)                {                    for(k=0;k<=8;k++)                    {                        if(val[i]==k)                          dp[b][j][S][k]=min(dp[b][j][S][k],dp[a][j][S][k]);                        else                        {                            dp[b][j][S|(1<<(val[i]-1))][val[i]]=min(dp[b][j][S|(1<<(val[i]-1))][val[i]],                                                                    dp[a][j][S][k]+1);                            dp[b][j+1][S][k]=min(dp[b][j+1][S][k],dp[a][j][S][k]);                        }                    }                }            }        }        ans=INF;        for(j=0;j<=m;j++)           for(S=0;S<len;S++)              for(k=0;k<=8;k++)                  ans=min(ans,dp[b][j][S][k]+ret-num[S]);        printf("Case %d: %d\n\n",++t,ans);    }}




0 0