Codeforces Round #384 (Div. 2) E. Vladik and cards


E. 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.


题意:给你n个数,范围1~8,求一个序列。满足条件1:每种数字之间个数差不超过1,例如数字1的个数和数字2的个数不能超过1,条件2:每种数字要连续出现,也就是只能是1122而不能是1221。求序列的最大长度。

思路:这题不看题解一点思路都没有。解法是这样的,满足条件的序列必定是有a种数的个数为x,8-a种数的个数为x+1,我们二分这个x,用状压dp判断是否满足有满足条件的,然后记录答案就可以了。状压dp的思路是这样的,首先预处理处每种数字第k次出现的位置。dp[i][j]表示终点为i,状态为j的出现(x+1)次的数的种类有几个,很容易得出转移方程,详情看代码:

#include<bits/stdc++.h> 
using namespace std;
typedef long long LL;
#define inf 0x3f3f3f3f3f3f3f3f
const int maxn=1005;
vector<int>v[15];
int n,a[maxn];
int dp[maxn][(1<<8)+5];
int solve(int x){
	memset(dp,-1,sizeof(dp));
	dp[0][0]=0;
	int now[15]={0};
	for(int i=0;i<=n;i++){
		now[a[i]]++;
		for(int j=0;j<(1<<8);j++){
			if(~dp[i][j]){
				for(int k=0;k<8;k++){
					if((j&(1<<k))||v[k+1].size()-now[k+1]<x)
						continue;
					int nextstatus=j|(1<<k);
					int nextindex=i;
					if(now[k+1]+x-1>=0)
						nextindex=nextindex=v[k+1][now[k+1]+x-1];
					dp[nextindex][nextstatus]=max(dp[nextindex][nextstatus],dp[i][j]);
					if(v[k+1].size()-now[k+1]>=x+1){
						nextindex=v[k+1][now[k+1]+x];
						dp[nextindex][nextstatus]=max(dp[nextindex][nextstatus],dp[i][j]+1);
					}
				}
			}
		}
	}
	int ans=-1;
	for(int i=1;i<=n;i++){
		ans=max(ans,dp[i][(1<<8)-1]);
	}
	if(~ans)
		return ans*(x+1)+(8-ans)*x;
	else
		return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		v[a[i]].push_back(i);
	}
	int l=0,r=n;
	int ans;
	while(l<=r){
		int mid=l+r>>1;
		int now=solve(mid);
		if(now>0){
			ans=now;
			l=mid+1;
		}
		else
			r=mid-1;
	}
	printf("%d\n",ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值