大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[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. POJ 1191 棋盘分割(区间DP)题解

    题意:中文题面 思路:不知道直接暴力枚举所有情况行不行... 我们可以把答案转化为 所以答案就是求xi2的最小值,那么我们可以直接用区间DP来写.设dp[x1][y1][x2][y2][k]为x1 y ...

  2. (转)Spring Cloud(一)

    (二期)22.微服务框架spring cloud(一) [课程22]spirng c...简介.xmind54KB [课程22]spirng cl...架构.xmind0.5MB [课程22]负载均. ...

  3. How to know the directory size in CENTOS 查看文件夹大小

    Under any linux system, you want to use the command du. (Disk Usage) Common usage is : du -sh file(s ...

  4. Attribute2Image --- Conditional Image Generation from Visual Attributes 论文笔记

     Attribute2Image --- Conditional Image Generation from Visual Attributes Target: 本文提出一种根据属性生成图像的产生式模 ...

  5. MVC ---- 怎删改查

    using Modelsop; using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  6. JS中什么是发布--订阅模式?

    转载文章部分内容: 发布订阅模式介绍 发布---订阅模式又叫观察者模式,它定义了对象间的一种一对多的关系,让多个观察者对象同时监听某一个主题对象,当一个对象发生改变时,所有依赖于它的对象都将得到通知. ...

  7. _event_active

    EventId 事件ID GUID 对应creature或gameobject表中 guid,正数为生物,负数为物体 ActiveFlag 生物或物体激活时的flag,通常为0 NoticeText ...

  8. _itemmod_creation_enchant

    该表控制物品产生时自动获得随机附魔效果 comment  备注  entry  物品entry  slot  附魔位置1-5,这些位置都可以用来产生自带附魔效果  groupId  附魔组Id 对就_ ...

  9. 使用ajax无法跨源问题总结

    参考文章: 浏览器同源政策及其规避方法 跨域资源共享 CORS 详解 使用jQuery实现跨域提交表单数据 <form action="http://v.juhe.cn/weather ...

  10. win10上安装keras

    下载Anaconda https://www.anaconda.com/ 点击进入下载界面 选择Windows版本64位,python3.7 下载完成后 ,双击安装 等待安装完成! 安装MinGW包, ...