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 真是题意杀啊, ...
随机推荐
- UOJ Round #1 [数论 | DP 排列]
UOJ Round #1 难度很良心啊! 做出了前两题,第三题看到仙人掌就吓哭了. [UR #1]缩进优化 就是求 \[ \sum_{i=1}^n a_i - (x-1)\sum_{i=1}^n\lf ...
- tcp 网络编程
网络编程同时也是进程间的一种通信:服务器进程和应用进程间的通信. OSI:开放式系统互联 OSI 7层模型: ...
- 架构师入门:搭建基本的Eureka架构(从项目里抽取)
没有废话,直接上干货,理论部分大家可以看其它资料. 这里是部分关键代码,如果需要全部可运行的代码,请给本人留言. 在后继,还将给出搭建高可用Eureka架构的方式. 1 Eureka的框架图 在Eur ...
- Java经典编程题50道之四十七
读取7个数(1~50)的整数值,每读取一个值,程序打印出该值个数的*. public class Example47 { public static void main(String[] arg ...
- SDL 2.0 如何在 windows 上使用?
https://wiki.libsdl.org/APIByCategory http://adolfans.github.io/sdltutorialcn/sdl-2-dot-0-tutorial-i ...
- Redis笔记1-redis的搭建和使用
1. Redis的安装 1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: ...
- 命令行下编译Wordcount
1. 编辑WordCount.java文件,在下载的hadoop安装包里有WordCount的例子 http://mirrors.hust.edu.cn/apache/hadoop/common/ha ...
- 沉淀,再出发——在Ubuntu Kylin15.04中配置Hadoop单机/伪分布式系统经验分享
在Ubuntu Kylin15.04中配置Hadoop单机/伪分布式系统经验分享 一.工作准备 首先,明确工作的重心,在Ubuntu Kylin15.04中配置Hadoop集群,这里我是用的双系统中的 ...
- 暴力破解MD5的实现(MapReduce编程)
本文主要介绍MapReduce编程模型的原理和基于Hadoop的MD5暴力破解思路. 一.MapReduce的基本原理 Hadoop作为一个分布式架构的实现方案,它的核心思想包括以下几个方面:HDFS ...
- linux 邮件服务器
邮件通信系统协议及概念:软件角色:MUA:邮件客户端MTA:邮件服务端MDA:邮件服务端模块邮件客户端:Mail User Agent,邮件用户代理邮件服务端:Mail Transfer Agent, ...