题意

给你一个1−8的排列,求一个满足条件的最长子序列
每种数字的差小于等于1,并且每种数字之内是连续的

解法

首先单纯认为用dp肯定不行的
所以应该考虑二分答案(所求长度具有二分性)
再用dp判断是否可行,这个dp很简单就是dp[N][1<<8]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f; int n;
vector<int> in[10];
int dp[N][300];
int a[N];
int cur[10];
void gmax(int &a, int b) {
if(a < b) a = b;
}
int ok(int len) {
for(int i = 0; i < 10; ++i) cur[i] = 0;
memset(dp,-1,sizeof(dp));
dp[0][0] = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < 256; ++j) {
if(dp[i][j] == -1) continue;
for(int k = 0; k < 8; ++k) {
if(j>>k&1) continue;
int tt = cur[k]+len-1;
if(tt < in[k].size()) gmax(dp[in[k][tt]+1][j | (1<<k)], dp[i][j]);
if(tt+1 < in[k].size()) gmax(dp[in[k][tt+1]+1][j | (1<<k)], dp[i][j]+1);
}
}
cur[a[i]-1] ++;
}
int ans = -1;
for(int i = 0; i <= n; ++i) gmax(ans, dp[i][255]);
if(ans == -1) return 0;
else return ans*(len+1) + (8-ans)*len;
}
int main(){
while(~scanf("%d",&n)) {
for(int i = 0; i < 10; ++i) in[i].clear();
for(int i = 0; i < n; ++i) {
scanf("%d",&a[i]);
in[a[i]-1].push_back(i);
}
int l = 1, r = n/8;
while(l <= r) {
int mid = (l+r)>>1;
if(ok(mid)) l = mid+1;
else r = mid-1;
}
int ans = max(ok(l), ok(r));
if(ans == 0) {
for(int i = 0; i < 8; ++i) {
if(!in[i].empty())
ans ++;
}
}
printf("%d\n",ans);
}
return 0;
}

CF384 div2 E. Vladik and cards的更多相关文章

  1. Codeforces Round #384 (Div. 2) 734E Vladik and cards

    E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp

    E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...

  3. Vladik and cards

    Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. [codeforces743E]Vladik and cards

    E. Vladik and cards time limit per test  2 seconds memory limit per test  256 megabytes input standa ...

  5. codeforces 235 div2 A. Vanya and Cards

    Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer ...

  6. Vladik and cards CodeForces - 743E (状压)

    大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...

  7. CodeForces743E. Vladik and cards 二分+状压dp

    这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有 ...

  8. 【codeforces 743E】Vladik and cards

    [题目链接]:http://codeforces.com/problemset/problem/743/E [题意] 给你n个数字; 这些数字都是1到8范围内的整数; 然后让你从中选出一个最长的子列; ...

  9. Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM _DONE

    不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串  相同之间随便飞 没有费用 不同的飞需要费用为  abs i-j 真是题意杀啊, ...

随机推荐

  1. [置换群&Polya计数]【学习笔记】

    昨天看了一下午<组合数学>最后一章然后晚上去看别人的blog发现怎么都不一样,我一定是学了假的polya 其实是一样的,只不过<组合数学>没有太多的牵扯群论.于是又从群论角度学 ...

  2. 链表回文判断(C++)

    题目描述: 对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构. 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构.保证链表长度小于等 ...

  3. 【linux之bash】

    bash的发展 1974年 贝尔实验室 Bourne Bourne Shell --> Bsh.sh 1978年 berke bill jey C shell --> Csh tcsh 8 ...

  4. Centos6.8安装zabbix-3.2.6

    系统环境:Centos6.8 mini软件:zabbix-3.2.6 zabbix 服务端安装 安装yum 扩展源 rpm -ivh http://repo.webtatic.com/yum/el6/ ...

  5. 关于DOM与BOM的总结

    1.什么是BOM,什么是DOM(基本概念) BOM: Browers Object MOdel           浏览器对象模型 DOM: Document Object MOdel         ...

  6. PowerDesigner Constraint name uniqueness 问题处理(转载)

    使用PowerDesigner生成数据库脚本时报 Constraint name uniqueness 错误: 双击每行错误,发现外键引用的名字有重复的: 惯性去网上找解决办法,找到的主要是两个方法: ...

  7. ElasticSearch和solr的差别

    Elasticsearch简介 Elasticsearch是一个实时分布式搜索和分析引擎.它让你以前所未有的速度处理大数据成为可能.它用于全文搜索.结构化搜索.分析以及将这三者混合使用:维基百科使用E ...

  8. 四级地址插件升级改造(京东商城地址选择插件)city-picker

    最近公司做的项目要和京东的数据做对接,所以要做个类似京东商品的详情页.页面的数据,是可以从京东接口获取到的,但是地址插件选择的效果需要自己实现.前端的同事在之前的项目中,已经选择了一款地址插件(cit ...

  9. 编码问题 php字符编码转换类

    各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...

  10. SQL语句学习

    看似简单,但其实包含很多技巧思维 1.查询课程表中所有科目大于80的学生 select distinct name from student where name not in (select nam ...