【DP】 hdu3237 Help Bubu

Help Bubu

http://acm.hdu.edu.cn/showproblem.php?pid=3237



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

题意: 给你n本书,书的高度有8种(这个是重点)。然后你可以最多有m次操作,每次的操作,就是拿出一本书放到任意位置,最后求出最小的连续相同高度的段数。

题解:滚动数组+状态压缩

开始的时候想到了记录前i个,取j次和最后的书的高度s,但是怎么都想不出这么转移。后来看了别人的题解来知道还要有记录当前高度状态k。具体的可以看代码中的注释。


#include<cstdio>
#include<cstring>
using namespace std;
//dp[i][j][k][s],前i个位置,花费j次操作,k为为未拿出书的集合,最后一本书的高度为s
//不取走第i位  1.s==tall[i]  dp[i][j][k][s]=min(dp[i][j][k][s],dp[i-1][j][k][s]
//            2.s!=tall[i]  dp[i][j][k|(1<<tall[i])][tall[i]]=min(dp[i][j][k|(1<<tall[i])][tall[i]],dp[i-1][j][k][s]+1)
//取走第i位                  dp[i][j+1][k][s]=min(dp[i][j+1][k][s],dp[i-1][j][k][s])
#define min(a,b) ((a)<(b)?(a):(b))
#define inf ((1<<30)-1)
int dp[2][105][1<<8][10];
int tall[105],num[1<<8];
int main()
{
    int n,m,begin;
    //预处理
    memset(num,0,sizeof(num));
    for(int i=0; i<(1<<8); ++i)
        for(int j=0; j<8; ++j)
            if(i&(1<<j)) num[i]++;
    for(int cas=1;~scanf("%d%d",&n,&m);++cas)
    {
        if(n+m==0) break;
        begin=0;//初始高度状态
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&tall[i]);
            tall[i]-=25;
            begin=begin|(1<<tall[i]);
        }
        for(int i=0; i<=m; ++i)//取走的次数
            for(int j=0; j<(1<<8); ++j)//高度状态
                for(int k=0; k<=8; ++k)//最后的书的高度
                    dp[0][i][j][k]=inf;
        dp[0][0][0][8]=0;//起始点
        for(int i=1; i<=n; ++i)
        {
            for(int j=0; j<=m; ++j)
                for(int k=0; k<(1<<8); ++k)
                    for(int s=0; s<=8; ++s)
                        dp[i&1][j][k][s]=inf;
            for(int j=0; j<i&&j<=m; ++j)
                for(int k=0; k<(1<<8); ++k)
                    for(int s=0; s<=8; ++s)
                        if(dp[(i-1)&1][j][k][s]!=inf)
                        {
                            //取走第i位
                            if(j<m)        dp[i&1][j+1][k][s]=min(dp[i&1][j+1][k][s],dp[(i-1)&1][j][k][s]);
                            //不取走第i位且高度与前一个相同
                            if(s==tall[i]) dp[i&1][j][k][s]=min(dp[i&1][j][k][s],dp[(i-1)&1][j][k][s]);
                            //不取走第i位且高度与前一个不同
                            else           dp[i&1][j][k|(1<<tall[i])][tall[i]]=min(dp[i&1][j][k|(1<<tall[i])][tall[i]],dp[(i-1)&1][j][k][s]+1);
                        }
        }
        int ans=inf;
        for(int i=0; i<=m; ++i)
            for(int j=0; j<(1<<8); ++j)
                for(int k=0; k<8; ++k)
                    if(dp[n&1][i][j][k]!=inf)
                    {
                        int temp=j^begin;//不在此状态且拿出的书的高度
                        if(ans>dp[n&1][i][j][k]+num[temp])
                            ans=dp[n&1][i][j][k]+num[temp];
                    }
        printf("Case %d: %d\n\n",cas,ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值