Vladik and cards
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:

  • the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are ck cards with number k on them in the subsequence, than for all pairs of integers  the condition |ci - cj| ≤ 1 must hold.
  • if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence[1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition.

Please help Vladik to find the length of the longest subsequence that satisfies both conditions.

Input

The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.

The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.

Output

Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.

Examples
input
3
1 1 1
output
1
input
8
8 7 6 5 4 3 2 1
output
8
input
24
1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8
output
17
Note

In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.

分析:状压dp;

   dp[i][j]表示到i为止j里面二进制1表示这个位置的数是否用过的取len+1的个数的最大值;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(int&p,int q){if(p<q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,pos[][maxn],dp[maxn][<<],cur[],a[maxn],ans;
bool ok(int len)
{
memset(dp,-inf,sizeof(dp));
memset(cur,,sizeof(cur));
dp[][]=;
bool flag=false;
for(int i=;i<=n;i++)
{
for(int j=;j<(<<);j++)
{
if(dp[i][j]==-inf)continue;
for(int k=;k<=;k++)
{
if(j&(<<(k-)))continue;
if(cur[k]+len>pos[k][])continue;
int now_pos=pos[k][cur[k]+len];
umax(dp[now_pos][j|(<<(k-))],dp[i][j]);
if(cur[k]+len+>pos[k][])continue;
now_pos=pos[k][cur[k]+len+];
umax(dp[now_pos][j|(<<(k-))],dp[i][j]+);
}
}
cur[a[i]]++;
}
for(int i=;i<=n+;i++)
{
if(dp[i][(<<)-]>=)
{
flag=true;
ans=max(ans,dp[i][(<<)-]*(len+)+(-dp[i][(<<)-])*len);
}
}
return flag;
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)a[i]=read();
rep(i,,n)
{
pos[a[i]][++pos[a[i]][]]=i;
}
rep(i,,)if(pos[i][])ans++;
int l=,r=n/;
while(l<=r)
{
int mid=l+r>>;
if(ok(mid))l=mid+;
else r=mid-;
}
printf("%d\n",ans);
//system("Pause");
return ;
}

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. [codeforces743E]Vladik and cards

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

  4. CF384 div2 E. Vladik and cards

    题意 给你一个的排列,求一个满足条件的最长子序列 每种数字的差小于等于,并且每种数字之内是连续的 解法 首先单纯认为用肯定不行的 所以应该考虑二分答案(所求长度具有二分性) 再用dp判断是否可行,这个 ...

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

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

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

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

  7. 【codeforces 743E】Vladik and cards

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

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

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

  9. 「算法笔记」状压 DP

    一.关于状压 dp 为了规避不确定性,我们将需要枚举的东西放入状态.当不确定性太多的时候,我们就需要将它们压进较少的维数内. 常见的状态: 天生二进制(开关.选与不选.是否出现--) 爆搜出状态,给它 ...

随机推荐

  1. CentOS-6.5x64:SSH安装配置

    1.CentOS 默认已经安装了 OpenSSH 2.vim /etc/ssh/sshd_config Port: SSH的监听端口 默认为22,设置为[Port 22] Protocol:SSH允许 ...

  2. join多表连接和group by分组

    join多表连接和group by分组 上一篇里面我们实现了单表查询和top N查询,这一篇我们来讲述如何实现多表连接和group by分组. 一.多表连接 多表连接的时间是数据库一个非常耗时的操作, ...

  3. 【Unity 3D】教程(1)建立场景

    1.新建一个地形 在菜单中选择Terrain,新建一个地形 接下来在右边的“编辑高度”中,用笔刷绘出地形高度,如图: 2.地形纹理 接下来我们给地形贴上纹理,纹理资源我们使用unity自带的, 在Pr ...

  4. ADO.NET—两种连接模式

    一.ADO.NET简介 ADO.NET的名称起源于ADO(ActiveX Data Objects),这是一个广泛的类组,用于在以往的Microsoft技术中访问数据.用来访问数据库,.NET环境下首 ...

  5. [ios2]iOS 图片与内存 【转】

    第一种解决方法:选择适当的加载方式 在程序的开发过程中,经常会用到很多的图片,适当的选择加载图片的方式就显得格外的重要,如果选择不得当,很容易造成内存吃紧而引起程序的崩溃. 这里介绍一下几种常见的加载 ...

  6. Float 的那些事

    css float 定义元素浮动到左侧或者右侧.其出现的本意是让文字环绕图片而已. left.right.inherit(从父级元素获取float值).none 一.浮动的性质 1. 包裹性 disp ...

  7. [UWP小白日记-12]使用新的Composition API来实现控件的阴影

    前言 看了好久官方的Windows UI Dev Labs示例好久才有点心得,真是头大.(其实是英语幼儿园水平(⊙﹏⊙)b) 真的网上关于这个API的资料可以说几乎没有. 正文 首先用这东西的添加WI ...

  8. mongodb tip-2

    1.or 查询的格式: var condition = {$or:[{field:1},{field:2}]} 2.字符串存储日期也可以用$gt $gte $lt $lte 直接比较 var cond ...

  9. Socket编程中 setsockopt的作用

    功能描述: 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值 ...

  10. 分享基于.NET MVC+EF CodeFirst+IOC+EasyUI的框架设计

    **注:要做工,没什么时间,等有空时会上传到GIT,项目结构如上,简单的说一下: **支持IOC及多数据库等,各项目由MVC区域隔离: 主要使用基于接口与抽象类进行高度的抽象与接口隔离,与其它框架比较 ...