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 ...
随机推荐
- IDEA 一次启动多个微服务模块项目
1,打开IDEA项目 .idea 下 的workspace.xml 2,查找“RunDashboard” 节点 3,添加如下内容 <option name="configuration ...
- IDEA 对spring boot Maven 项目打 Jar 包
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...
- Linux系统中使用Nignx配置反向代理负载均衡
目录 使用nginx实现动静分离的负载均衡集群 使用nginx实现负载均衡和动静分离 使用nginx实现动静分离的负载均衡集群 Nginx官网源码包下载链接:http://nginx.org/en/d ...
- C语言--浮点数
在程序中使用浮点数 -- 浮点数的精确性有限 -- 在从c语言中float类型的精确度只到小数点的7位 -- 浮点数只能在一定范围内去相信它 -- 在有精确度高的要求下不要使用浮点数(在算钱的时候,误 ...
- C++中的单例类模板
1,本节课讲述单例类模式,实现并抽取相关代码实现单例类模板,在以后开发工作 中,如果想要使用单例模式,那么直接使用今天开发的单例类模板就可以: 2,需求的提出: 1,在架构设计时,某些类在整个系统生命 ...
- 树形DP水题系列(1):FAR-FarmCraft [POI2014][luogu P3574]
题目 大意: 边权为1 使遍历树时到每个节点的时间加上点权的最大值最小 求这个最小的最大值 思路: 最优化问题 一眼树形DP 考虑状态设立 先直接以答案为状态 dp[u] 为遍历完以u为根的子树的答案 ...
- vim下使用UltiSnips
vim下使用UltiSnips 最近在学习Linux编程,相应的也在学vim.vim中的UltiSnips插件可以实现比一般的代码补全更为强大的效果.这里就记录一下吧. UltiSnips安装.基础及 ...
- CDH部署StreamSets
StreamSets是一个大数据采集工具,数据源支持包括结构化和半/非结构化,目标源支持HDFS,HBase,Hive,Kudu,Cloudera Search, ElasticSearch等.它包括 ...
- 81. Search in Rotated Sorted Array II (JAVA)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- java 判断5张牌的组成
题目: 一副牌中发五张扑克牌给你,判断是四条,三带二.三带一加一.两对.一对.顺子.还是什么都不是. 控制台输入: 1,1,1,1,2 示例输出: 四条 Java方法的代码: static Strin ...