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 真是题意杀啊, ...
随机推荐
- (转)Unity控制反转和依赖注入
昨天,面试官说他们的项目使用的是Unity,我们的项目中使用的是autofac,看了一下,用法都差不多,就连方法的名字都是一样的哈,想了解的朋友可以看看这篇文章,作者讲解的挺详细的,关于autofac ...
- FindBugs简单应用
FindBugs是一种java代码的静态分析工具,无需开发人员费劲就能找出代码中可能存在的缺陷.FindBugs 不注重样式或者格式,它试图只寻找缺陷或者潜在的性能问题. 第一步,http://sou ...
- [SCOI2010]幸运数字 [容斥原理 dfs]
题意:"幸运号码"是十进制表示中只包含数字6和8的那些号码,求\([l,r]:r \le 10^10\)之间"幸运号码"的倍数个数 发现幸运号码貌似很少唉,去掉 ...
- BZOJ 2303: [Apio2011]方格染色 [并查集 数学!]
题意: $n*m:n,m \le 10^6$的网格,每个$2 \times 2$的方格必须有1个或3个涂成红色,其余涂成蓝色 有一些方格已经有颜色 求方案数 太神了!!!花我三节课 首先想了一下只有两 ...
- .net使用AsposeWord导出word table表格
本文为原创,转载请注明出处 1.前言 .net平台下导出word文件还可以使用Microsoft.Office.Interop和NPOI,但是这两者都有缺点,微软的Office.Interop组件需要 ...
- java Mac自动化-java和ant环境搭建
本文旨在帮助读者介绍,如果一个测试工程师拿到了mac本,该如何在本地搭建java和ant环境 其实在几年前,我们还大多使用的是windows本,而且我们也会比较善于使用windows笔记本,但自从ma ...
- Windows下Nginx的配置及配置文件部分介绍
一.在官网下载 nginx的Windows版本,官网下载:http://nginx.org/download/ 选择你自己想要的版本下载,解压 nginx(例如nginx-1.6.3) 包到你的win ...
- Leetcode刷题C#版之Toeplitz Matrix
题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...
- css中的关于margin-top,position和z-index的一些bug解决方案
这两天在写一个demo的时候,就碰到一些css的问题,不知道能不能算bug,很有可能是因为我写的代码太少,孤陋寡闻了_(:зゝ∠)_.记录一下,以防下次遇到同样问题. 进入正题: 1.问题描述:div ...
- aria2 加速百度网盘下载
准备工作: chrome浏览器: BaiduExporter插件(下载地址:https://github.com/acgotaku/BaiduExporter): aria2工具(下载地址:http: ...