【题目链接】:http://codeforces.com/problemset/problem/743/E

【题意】



给你n个数字;

这些数字都是1到8范围内的整数;

然后让你从中选出一个最长的子列;

要求这个子列中各个数字出现的次数的差的绝对值都不超过1;

且如果是相同的数字的话:

都是连在一起的(不会有分散的数字);

问你这个最长的序列的长度是多少;

【题解】



二分每个数字至少出现的次数x,(即最少出现x次,当然也可以是x+1次);

(单调性是显然的吧,因为如果每个数字出现5次是可行的话,那么3次肯定也是可以的,二分是正确的);

然后判断这样的序列存不存在;

可以一个数字一个数字的判断;

对于每个数字而言.

只有两种情况.在这个序列中,或者不在;

(想要加入到这个序列中,

则必须之前没有出现过,

因为题目有要求不能出现分散的块;

如果之前没有出现过;

则加入到这个序列中,

枚举它要出现x次,还是x+1次,

则可以写个lower_bound快速判断它之后的

第x个该数字(或是x+1)的位置,然后再从那个位置后一位开始,

继续选择,同时记录刚才那个数字已经加入到序列中;

(同时序列的长度递增x或是x+1)

当然也可以不选这个数字,则往后移动一位;

到了第n+1个位置的时候,看看是不是所有的8个数字都选了;

是的话则返回找到了一个序列,否则返回一个很小的值就好;

这些都用记忆化搜索写吧.

开个

f[i][s],表示当前扫描到第i个位置,当前选择的数字的状态为s;(二进制对应);

的最大序列长度;

bo[i][s]记录这个状态有没有找过.

i最大1000,s最大2^8=256

状态这么少,怎么样都不会超啦;

然后如果能找到x的序列,就尝试把x搞大一点,继续找;

然后x为0的情况没办法在做记搜的时候体现出来;

得自己一开始的时候预处理出ans



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const int MM = 1<<8;
const int INF = -0x3f3f3f3f; int n,a[N],mark,ans,f[N][MM+10];
bool bo[N][MM+10];
vector <int> v[10]; int dfs(int i,int s,int x)
{
if (bo[i][s]) return f[i][s];
bo[i][s] = true;
if (i>n)
{
if (s==MM-1)
{
return f[i][s] = 0;
}
else
return f[i][s] = INF;
}
int ret = INF;
if (!((s>>(a[i]-1))&1))
{
int pos = lower_bound(v[a[i]].begin(),v[a[i]].end(),i)-v[a[i]].begin();
if (pos+x-1<int(v[a[i]].size()))
{
ret = max(ret,x+dfs(v[a[i]][pos+x-1]+1,s|(1<<(a[i]-1)),x));
}
if (pos+x<int(v[a[i]].size()))
{
ret = max(ret,x+1+dfs(v[a[i]][pos+x]+1,s|(1<<(a[i]-1)),x));
}
}
f[i][s] = max(ret,dfs(i+1,s,x));
return f[i][s];
} bool ok(int x)
{
ms(f,INF);
ms(bo,0);
int ret = INF;
ret = max(ret,dfs(1,0,x));
ans = max(ans,ret);
if (ret>=0)
return 1;
else
return 0;
} int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
rep1(i,1,n)
{
cin >> a[i];
if(!((mark>>a[i])&1))
{
mark|=(1<<a[i]);
ans++;
}
v[a[i]].pb(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;
}
cout << ans << endl;
return 0;
}

【codeforces 743E】Vladik and cards的更多相关文章

  1. 【35.02%】【codeforces 734A】Vladik and flights

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【44.64%】【codeforces 743C】Vladik and fractions

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【codeforces 777B】Game of Credit Cards

    [题目链接]:http://codeforces.com/contest/777/problem/B [题意] 等价题意: 两个人都有n个数字, 然后两个人的数字进行比较; 数字小的那个人得到一个嘲讽 ...

  7. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【47.40%】【codeforces 743B】Chloe and the sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. 06006_redis数据存储类型——String

    1.概述 (1)字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等: (2)在Redis中 ...

  2. asp.net core 2.1开发环境下配置IIS

    asp.net core 2.1是可以在开发环境下配置到IIS中的,这样就可以在vs中按F5直接运行了 在项目上右键点击属性,然后在Debug中配置如下参数 首先点击New按钮,输入IIS,点击确定 ...

  3. Impala ODBC 安装笔记

    Impala在线文档介绍了 Impala ODBC接口安装和配置 http://www.cloudera.com/content/cloudera-content/cloudera-docs/CDH5 ...

  4. Java測试覆盖率工具----Cobertura,EclEmma

    Cobertura 是一个与Junit集成的代码覆盖率測量工具 它是免费.开源的 它能够与Ant和Maven集成.也能够通过命令行调用 能够生成HTML或XML格式的报告 能够依照不同的标准对HTML ...

  5. UNIX环境高级编程(5):文件I/O(1)

    UNIX系统中的大多数文件I/O仅仅须要用到5个函数:open.read.write.lseek以及close.本章说明的函数常常称为"不带缓冲的I/0",术语不带缓冲指的是每一个 ...

  6. 四、基于HTTPS协议的12306抢票软件设计与实现--水平DNS并发查询分享

    一.基于HTTPS协议的12306抢票软件设计与实现--实现效果 二.基于HTTPS协议的12306抢票软件设计与实现--相关接口以及数据格式 三.基于HTTPS协议的12306抢票软件设计与实现-- ...

  7. unity3d面试题摘选(全)

    ======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...

  8. UVA 1515 Pool construction 最大流跑最小割

    Pool construction You are working for the International Company for Pool Construction, a constructio ...

  9. Unable to access the IIS metabase

    https://stackoverflow.com/questions/12859891/error-unable-to-access-the-iis-metabase 解决方法1 On Window ...

  10. datatable dataRow

    DataRow[] Drs = DtStockProduct.Select(Condition11); DtResult = DtStockProduct.Clone(); datatble tabl ...