传送门

题目大意

有n本书,最多k次操作,每次操作可以把一本书拿出来,放到一个位置去,有一个指标较mess度,他是书的高度的段数,连续的书高度一样算一段,现在给你最先开始各个位置上的书的高度,求操作后最小的mess度。

分析

首先我们要注意一个非常非常重要的条件就是书的高度的范围很小。所以我们不由想到了状压dp。我们再仔细思考一下不难想到dp[i][j][msk][k]表示考虑到第i本,挪动了j次,没挪动的书的高度集合为msk,没挪动的书的最后一本高度为k。然后我们便可以转移(具体见代码),而最后答案要将dp值加上对于所有高度为h的书都挪动了的不同h的数量。注意数组不要开小啦。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define gm(x,y) x=min(x,y)
const int inf = 0x3f3f3f3f;
int n,m,sum,dp[][][][],apr[],c[],cse,now;
inline void init(){
memset(apr,,sizeof(apr));
sum=;
cse++;
}
inline void deal(int pl,int x){
if(!apr[x]){
sum++;
apr[x]=sum;
}
c[pl]=apr[x];
return;
}
inline int Sum(int msk){
int res=;
for(int i=;i<sum;i++)
if(((<<i)&msk)==)
res++;
return res;
}
inline void DP(){
int i,j,k,s;
now=;
memset(dp[],0x3f,sizeof(dp[]));
dp[][][(<<(c[]-))][c[]]=;
dp[][][][]=;
for(i=;i<n;i++){
now^=;
memset(dp[now],0x3f,sizeof(dp[now]));
for(j=;j<=m;j++)
for(s=;s<(<<sum);s++)
for(k=;k<=sum;k++)if(dp[now^][j][s][k]<inf){
//cout<<i<<' '<<j<<' '<<s<<' '<<k<<' '<<dp[now^1][j][s][k]<<endl;
if(k==c[i+])
gm(dp[now][j][s][k],dp[now^][j][s][k]);
else
gm(dp[now][j][s|(<<(c[i+]-))][c[i+]],dp[now^][j][s][k]+);
if(j<m)gm(dp[now][j+][s][k],dp[now^][j][s][k]);
}
}
return;
}
inline void getans(){
int ans=inf,i,j,k;
for(i=;i<=m;i++)
for(j=;j<(<<sum);j++)
for(k=;k<=sum;k++)
gm(ans,dp[now][i][j][k]+Sum(j));
printf("Case %d: %d\n\n",cse,ans);
}
int main(){
int i,j,k;
cse=;
scanf("%d%d",&n,&m);
while(n&&m){
init();
for(i=;i<=n;i++){
scanf("%d",&c[i]);
deal(i,c[i]);
}
DP();
getans();
scanf("%d%d",&n,&m);
}
return ;
}

Help Bubu UVALive - 4490的更多相关文章

  1. UVALive 4490 Help Bubu

    题目大意:有n本书,高度值域为8,现可以把k本书拿出来再放进去,相邻的.高度相同的书算作一块,最小化块的个数.n=100. 强烈建议大家不要在做完区间DP后做别的DP题:区间DP是整体考虑,而一般DP ...

  2. UVALive 4490 压缩DP

    转载自http://blog.csdn.net/zstu_zlj/article/details/9903589 没有接触过压缩DP.位运算也不太熟.所以理解了思路还是不懂代码.

  3. UVA Live Archive 4490 Help Bubu(状压dp)

    难点在于状态设计,从左向右一本书一本书的考虑,每本书的决策有两种拿走或者留下, 对于拿走后的书,之后要放回,但是决策过程中不知道到往哪里放, 虽然前面的书的种类确定,可能是往后面放更优,而后面的书的类 ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  8. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. mosquitto配置文件说明

    安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明. # ======== ...

  2. Shell check IP

    #! /bin/bash checkip() {        if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 ...

  3. Java演示手机发送短信验证码功能实现

    我们这里采用阿里大于的短信API 第一步:登陆阿里大于,下载阿里大于的SDK a.在阿里大于上创建自己的应用 b.点击配置管理中的验证码,先添加签名,再配置短信模板 第二步:解压相关SDK,第一个为j ...

  4. leetcode_sql_2,183

    183. Customers Who Never Order Suppose that a website contains two tables, the Customers table and t ...

  5. linux大于2T的磁盘格式化

    fdisk默认只能格式小于2T的磁盘,我们经常会碰到大于2T的磁盘,我们不能fdisk 格式化. 我们得用parted 来的格式化 parted 命令可能没有,yum install -y parte ...

  6. LeetCode Count Binary Substrings

    原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...

  7. DIV设置浮动float以后下一个DIV要换行的方法

    <div style=“float:left;”> 1111111 </div> <div style=“float:left;”>222222 </div& ...

  8. 扒站工具Teleport Pro教程

    1.下载软件 http://www.jb51.net/softs/44134.html 2.安装 3.界面 先点开帮助点注册(类似于破解要不全站扒不全) 下面请看ppt, http://www.doc ...

  9. Java 虚拟机-Java内存区域

    简要介绍Java的内存区域: 运行时数据区域 HotSpot虚拟机对象 一.概览 二.运行时数据区域 2.1 程序计数器 Program Counter Register,代表当前线程所执行的字节码的 ...

  10. JSP Servlet中的Request和Response的简单研究

    本文参考了几篇文章所得,参考目录如下: 1.http://www.cnblogs.com/guangshan/p/4198418.html 2.http://www.iteye.com/problem ...