大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续.

正解是状压dp, 先二分转为判断[1,8]出现次数>=x是否成立, 再dp求出前i位匹配状态S长度为x+1的数字个数的最大值, 特判一下最低次数为0的情况. 这题打了好久, 太菜了.......

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef bitset<10> btc;
const int P = 1e9+7, INF = 0xbcbcbcbc;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e3+10, S = (1<<8)-1;
int n, ans;
int a[N], dp[N][S+1], dig[S+1], f[N][N][9], g[S+1];
void chkmax(int &x, int y) {x=max(x,y);}
void chkmin(int &x, int y) {x=min(x,y);}
int chk(int x) {
memset(dp, 0xbc, sizeof dp);
dp[0][0] = 0;
REP(i,1,n) REP(j,0,S-1) if (dp[i-1][j]!=INF) {
for (int k=~j&S, t; k; k^=t) {
t = k&-k;
int p1 = f[i][x][dig[t]], p2 = f[i][x+1][dig[t]];
if (p1<=n) chkmax(dp[p1][j^t], dp[i-1][j]);
if (p2<=n) chkmax(dp[p2][j^t], dp[i-1][j]+1);
}
}
int r = INF;
REP(i,1,n) chkmax(r,dp[i][S]);
ans = max(ans, r+8*x);
return r!=INF;
} int main() {
REP(i,0,7) dig[1<<i]=i+1;
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i);
memset(f,0x3f,sizeof f);
PER(i,1,n) REP(j,1,n) REP(k,1,8) {
if (a[i]==k) f[i][1][k]=i,f[i][j][k]=f[i+1][j-1][k];
else chkmin(f[i][j][k],f[i+1][j][k]);
}
g[0] = 1;
REP(i,1,n) REP(j,0,S) if (!(j>>a[i]-1&1)) {
g[j^1<<a[i]-1] |= g[j];
}
REP(i,0,S) if (g[i]) ans=max(ans,__builtin_popcount(i));
int l=1, r=n/8;
while (l<=r) {
if (chk(mid)) l=mid+1;
else r=mid-1;
}
printf("%d\n", ans);
}

Vladik and cards CodeForces - 743E (状压)的更多相关文章

  1. Hongcow Buys a Deck of Cards CodeForces - 744C (状压)

    大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...

  2. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  3. Clear The Matrix CodeForces - 903F (状压)

    大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...

  4. Pollywog CodeForces - 917C (状压)

    链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...

  5. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  6. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

  7. Keyboard Purchase CodeForces - 1238E (状压)

    大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. ...

  8. Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...

  9. codeforces 1185G1 状压dp

    codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...

随机推荐

  1. 分布式知识点总结(来自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/%E5%88%86%E5%B8%83%E5%BC%8F.md 注:如Paxos等的 ...

  2. 不能安装64位office提示已安装32位的

    安装64位office办公软件的时候提示已经安装32位的office办公软件所以无法继续安装,但实际上之前安装的32位的office办公软件已经卸载了.问题现象截图如下: 从问题描述中,我们其实已经能 ...

  3. P3211 [HNOI2011]XOR和路径

    思路 看到异或,容易联想到二进制位之间是相互独立的,所以可以把问题变成每个二进制位为1的概率再乘上(1<<pos)的值 假设现在考虑到pos位,设f[i]为第i个节点期望的异或和第pos位 ...

  4. try里Response.end()问题

    问题 在xxx.aspx.cs中处理异步请求,大致代码如下: 但会发现始终会进catch. 原因 Response.End()会引发ThreadAbortException. 解决方案 使用HttpC ...

  5. Lintcode415-Valid Palindrome-Medium

    Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ...

  6. SAP固定资产业务场景及方案

    SAP固定资产业务场景及方案 http://mp.weixin.qq.com/s/hYlaNHJMQBTZpyFAmP2h3A 对于FICO应用资深专家或顾问,须业务场景及需求成竹在胸:对于非财务顾问 ...

  7. NOI1999 生日蛋糕

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; #define I ...

  8. 利用angularjs完成注册表单

    ng-init="username = 'first'"设置初始显示first字段 ng-class="{'error':signUpForm.username.$inv ...

  9. 2017"百度之星"程序设计大赛 - 复赛 01,03,05

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. VS IIS Express 支持局域网访问

    使用Visual Studio开发Web网页的时候有这样的情况:想要在调试模式下让局域网的其他设备进行访问,以便进行测试.虽然可以部署到服务器中,但是却无法进行调试,就算是注入进程进行调试也是无法达到 ...