CF384 div2 E. Vladik and cards
题意
给你一个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的更多相关文章
- 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 ...
- 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 ...
- Vladik and cards
Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- [codeforces743E]Vladik and cards
E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- Vladik and cards CodeForces - 743E (状压)
大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...
- CodeForces743E. Vladik and cards 二分+状压dp
这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有 ...
- 【codeforces 743E】Vladik and cards
[题目链接]:http://codeforces.com/problemset/problem/743/E [题意] 给你n个数字; 这些数字都是1到8范围内的整数; 然后让你从中选出一个最长的子列; ...
- Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM _DONE
不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串 相同之间随便飞 没有费用 不同的飞需要费用为 abs i-j 真是题意杀啊, ...
随机推荐
- MySQL连接数实时查看
MySQL连接数实时查看 1.查看当前所有连接详细信息,只显示10个 2.查看连接状态 箭头所指的地方一般最重要,表示当前的连接数有多少个 3.查看所有连接的详细信息 4.实时查看连接详细信息 这 ...
- Cookie、session和localStorage、以及sessionStorage之间的区别
一.Cookie.session和localStorage的区别 cookie的内容主要包括:名字.值.过期时间.路径和域.路径与域一起构成cookie的作用范围.若不设置时间,则表示这个cookie ...
- webrtc底层一对一连接过程探索(三)
一.连接过程继续解读-----fun33-fun35解读 完整代码如下: //fun33-37 console.error('fun35-37==>2332==>2332'); var q ...
- MIT-线性代数笔记(1-6)
学习目录 第 01 讲 行图像和列图像 第 02 讲 矩阵消元 第 03 讲 矩阵的乘法和逆矩阵 第 04 讲 矩阵的LU 分解 第 05 讲 转置.置换和空间 第 06 讲 列空间和零空间 第 07 ...
- yii2 源码分析 model类分析 (五)
模型类是数据模型的基类.此类继承了组件类,实现了3个接口 先介绍一下模型类前面的大量注释说了什么: * 模型类是数据模型的基类.此类继承了组件类,实现了3个接口 * 实现了IteratorAggreg ...
- Golang学习 - strconv 包--数据类型转换
// 将布尔值转换为字符串 true 或 false func FormatBool(b bool) string // 将字符串转换为布尔值 // 它接受真值:1, t, T, TRUE, true ...
- 940A Points on the line
传送门 题目大意 给你n和d还有n个数,计算最少删除几个点可以是最大点与最小点之差小于等于d 分析 先对所有点排序,枚举每一个点ai到ai+d中有几个点,答案即n-其中最大的值 代码 #include ...
- MySQL数据库基础(四)(子查询与链接)
1.子查询简介 其中,所谓的"外层查询"并不是指"查找",指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称 ...
- C/C++语言简介之语言特点
一.基本特性 1.高级语言:它是把高级语言的基本结构和语句与低级语言的实用性结合起来的工作单元. 2.结构式语言:结构式语言的显著特点是代码及数据的分隔化,即程序的各个部分除了必要的信息交 ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 配置选项
配置选项 在基于"less rope to hang yourself with"思想下,.NET 框架没有给开发提供很多太多的配置选项.但在大多数情况下,GC会跟你的硬件配置,及 ...