Pku2978 Colored stones
题目链接:Click here
Solution:
状压dp,考虑\(f[i][j][k]\)表示当前到了第i个石头,颜色状态为j,选取的最后一个石头颜色为k时能够留下的石头的最大数量
转移也很好转移,枚举所有状态,再枚举上次转移过来的状态的最后一个颜色,然后暴力转移就行了
最后查找一下所有情况下的最大值,输出n-max就行了
Code:
#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,f[101][(1<<8)-1][7];
int b[7]={0,1};
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int main(){
for(int i=2;i<=6;i++) b[i]=b[i-1]*2;
begin:n=read(),m=read();
if(n==0&&m==0) return 0;
memset(f,0,sizeof(f));
int num=b[m+1]-1,ans=0;
for(int i=1;i<=n;i++){
int x=read();
for(int s=0;s<=num;s++){
if(!(s&b[x]))
for(int j=1;j<=m;j++){
if(!(s&b[j])) continue;
f[i][s|b[x]][x]=max(f[i][s|b[x]][x],f[i-1][s][j]+1);
f[i][s][j]=max(f[i][s][j],f[i-1][s][j]);
}
else
for(int j=1;j<=m;j++){
if(j==x) f[i][s][x]=max(f[i][s][x],f[i-1][s][j]+1);
else if(s&b[j]) f[i][s][j]=max(f[i][s][j],f[i-1][s][j]);
}
}
}
for(int i=1;i<=m;i++)
for(int s=0;s<=num;s++)
if(s&b[i]) ans=max(ans,f[n][s][i]);
printf("%d\n",n-ans);goto begin;
}
Pku2978 Colored stones的更多相关文章
- HOJ 2156 &POJ 2978 Colored stones(线性动规)
Colored stones Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1759 Accepted: 829 Descrip ...
- [poj 2978]Colored Stones[状态压缩DP]
题意: 给出n个石子,一共m种颜色.问最少去掉几个石子使得同种颜色全连续. 思路见注释. #include <algorithm> #include <cstdio> #inc ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- HDU 5973 Game of Taking Stones 威佐夫博弈+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...
- 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...
- codeforces 553A . Kyoya and Colored Balls 组合数学
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
随机推荐
- 关于Docx动态控制word模板文件的数据
博客:https://www.cnblogs.com/24klr/ github: https://github.com/luoruiemail/Dynamic_Word_Web 参考资料:https ...
- Python 正则匹配网页内的IP地址及端口号
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-30 20:38:23 # @Author : EnderZhou (z ...
- nodejs 写服务器解决中文乱码问题
nodejs 写服务器解决中文乱码问题:https://blog.csdn.net/worldmakewayfordream/article/details/77483423 本文链接:htt ...
- Codeforces Round #589 (Div. 2) (e、f没写)
https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...
- gcc数据对齐之: howto 2.
原文链接:http://www.catb.org/esr/structure-packing/ 谁应阅读本文 本文探讨如何通过手工重新打包C结构体声明,来减小内存空间占用.你需要掌握基本的C语言知识, ...
- Linux免密登陆设置了免密登陆为啥还需要输入密码
一.设置了免密码登陆但是还是需要输入密码: 权限保证:1.authorized-keys 的权限为 600 2.home.账户所在的目录如hadoop..ssh这三个文件的权限都必须设置为700,缺少 ...
- JS中property与attribute的区别
property与attirbute都是属性的意思,在JS中很容易混淆,但实际上二者有很大的区别.简单来说, property:是DOM中的属性,是JavaScript中的对像 attribute:是 ...
- group by问题
在使用group by进行查询结果分组的时候,报错: 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contain ...
- oracle常用函数(1)
oracle有很强大的函数功能,常用的字符处理函数如下: SQL> select initcap('hello') from dual;//将首字母转为大写 INITCAP('HELLO')-- ...
- 最大熵与EM算法
一.熵.联合熵(相当于并集).条件熵.互信息 1.熵是什么? (0)信息量:信息的度量p(xi).信息量和概率成反比,熵是信息量的期望. X是一个随机变量,可能取值有很多个.熵是信息量的期望.熵反应的 ...