Help Bubu UVALive - 4490
题目大意
有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的更多相关文章
- UVALive 4490 Help Bubu
题目大意:有n本书,高度值域为8,现可以把k本书拿出来再放进去,相邻的.高度相同的书算作一块,最小化块的个数.n=100. 强烈建议大家不要在做完区间DP后做别的DP题:区间DP是整体考虑,而一般DP ...
- UVALive 4490 压缩DP
转载自http://blog.csdn.net/zstu_zlj/article/details/9903589 没有接触过压缩DP.位运算也不太熟.所以理解了思路还是不懂代码.
- UVA Live Archive 4490 Help Bubu(状压dp)
难点在于状态设计,从左向右一本书一本书的考虑,每本书的决策有两种拿走或者留下, 对于拿走后的书,之后要放回,但是决策过程中不知道到往哪里放, 虽然前面的书的种类确定,可能是往后面放更优,而后面的书的类 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- SpringCloud教程 | 第五篇: 路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...
- winform中DataGridView使用DataGridViewCheckBoxColumn实现RadioBox单选功能
private void dgvMaterial_CellContentClick(object sender, DataGridViewCellEventArgs e) { ; i < dgv ...
- hdu-2673-shǎ崽 OrOrOrOrz(水题)
注意输出格式 #include <iostream> #include <algorithm> using namespace std; +]; int main() { in ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- VMware12版虚拟机怎么安装win7系统(详细教程
转自:http://jingyan.baidu.com/article/cd4c29791fcf1b756e6e6034.html VMware12版虚拟机怎么安装win7系统(详细教程) 现 在很多 ...
- P1605 迷宫(洛谷)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...
- bzoj 2839 集合计数 —— 二项式反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2839 设 \( f(i) \) 为至少 \( i \) 个选择,则 \( f(i) = C_ ...
- Visualforce Page CSS样式
Salesforce Page开发者文档:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_stylin ...
- 新版本Ubuntu本地提权漏洞复现
该漏洞在老版本中被修复了,但新的版本还存在漏洞 影响范围:Linux Kernel Version 4.14-4.4,Ubuntu/Debian发行版本 Exp下载地址:http://cyseclab ...
- delphi 安卓配置教程
本教程以 delphi 10.2.2.2004 为例,演示 delphi 安卓配置步骤 1.打开 Android Tools 2. 选择合适的版本.比如:我的小米4 LTE 是 andorid 6.0 ...