传送门

题目大意

有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. java学习笔记 --- IO(2)

    IO流的分类:  流向:  输入流  读取数据  输出流 写出数据  数据类型: 字节流 字节输入流  读取数据 InputStream 字节输出流  写出数据 OutputStream 字符流 字符 ...

  2. Linux 线程同步的三种方法(互斥锁、条件变量、信号量)

    互斥锁 #include <cstdio> #include <cstdlib> #include <unistd.h> #include <pthread. ...

  3. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  4. C++ 静态常量

    #include<iostream> #include<stdexcept> #include <map> using namespace std; class n ...

  5. BZOJ3075,LG3082 [USACO13MAR]项链Necklace

    题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...

  6. [调试日志]用php函数var_export把多维数组file_put_contents写入并打印到日志,以方便调试之多维数组,用php5中的var_export函数示例,顺带介绍http_build_query(转)

    一行解决写入日志: file_put_contents("/tmp/jack.txt", var_export($layReturnArr,TRUE),FILE_APPEND); ...

  7. wpf控件提示Value ‘’ can not convert

    我们在对控件的ErrorTemplate设置后,有时会出现Value '' can not convert. 为什么会出现呢? 原因:如果控件的输入值和null不能转换(比如控件要求的是int或flo ...

  8. div+css制作带箭头提示框效果图(原创文章)

    一直都在看站友们的作品,今天也来给大家分享一个小的效果,第一次发还有点小紧张呢,语言表达能力不是很好,还请见谅…^ 先来个简单点的吧,上效果图 刚开始在网上看到效果图的时候感觉好神奇,当我试着写出来的 ...

  9. Python内置:items()方法

    文章转载于:https://www.cnblogs.com/wushuaishuai/p/7738118.html(博主:IT技术随笔) #Python3中已取消iteritems()方法 描述 Py ...

  10. 第三章 Java程序优化(待续)

    字符串优化处理 String对象及其特点 String对象是java语言中重要的数据类型,但它并不是Java的基本数据类型.在C语言中,对字符串的处理最通常的做法是使用char数组,但这种方式的弊端是 ...