Codeforces 743E(二分+状压dp)
2016-12-17 14:30
417 查看
E. Vladik and cards
题意:给定一个长度为n的序列,序列只有1-8这八个数字组成,要你找到一个最长的子序列,满足在找到的子序列中每个数字的数目相差不超过1,并且相同的数字在子序列中要求连续。题解:设子序列中每个数字的数量至少为len,其中长度为len+1的为k个,则子序列长度为k∗(len+1)+(8−k)∗len,通过二分枚举len,然后用状压dp判断是否可行并得到最大的k,设数组dp
[(1<<8)−1],在dp[i][s]中,s的二进制串中,1表示那个数字已经出现了至少len次,0表示没有,dp[i][s]表示在前i位中,s的二进制串为1那位的那个数字已经出现了至少len次。
代码:
#include <bits/stdc++.h> using namespace std; #define N 1010 vector<int> g[10]; int a ,p[10],cur[10],dp [1<<8]; int n; int check(int len) { memset(cur,0,sizeof(cur)); memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int i=0;i<n;++i) { for(int j=0;j<(1<<8);++j) { if(dp[i][j]==-1) continue; for(int k=1;k<=8;++k) { if(j&(1<<(k-1))) continue; int x=cur[k]+len-1; if(x>=g[k].size()) continue; dp[g[k][x]][j|(1<<(k-1))]=max(dp[g[k][x]][j|(1<<(k-1))],dp[i][j]); ++x; if(x>=g[k].size()) continue; dp[g[k][x]][j|(1<<(k-1))]=max(dp[g[k][x]][j|(1<<(k-1))],dp[i][j]+1); } } ++cur[a[i]]; } int ans=-1; for(int i=1;i<=n;++i) { //printf("%d ??\n",dp[i][(1<<8)-1]); ans=max(ans,dp[i][(1<<8)-1]); } if(ans==-1) return -1; return ans*(len+1)+(8-ans)*len; } int main() { scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); g[a[i]].push_back(i); } int s=1,t=n/8,mid; while(s+1<t) { mid=s+t>>1; if(check(mid)!=-1) s=mid; else t=mid-1; } int ans=max(check(s),check(t)); if(ans==-1) { ans=0; for(int i=1;i<=8;++i) if(g[i].size()) ++ans; } printf("%d\n",ans); return 0; }
相关文章推荐
- 最终还是水过去的解题报告
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- Codeforces Round #375 (Div. 2)C. Polycarp at the Radio
- POJ 3254 - Corn Fields [状压DP]
- Codeforces Educational Codeforces Round 16 部分题解
- 【WHUST 2016 Individual Contest #1】解题报告
- 【WHUST 2016 Individual Contest #2】解题报告
- 【WHUST 2016 Individual Contest #3 】解题报告
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #363 (Div. 2)
- Codeforces Round #265 (Div. 2)
- A.Bus to Udayland