Vladik and cards CodeForces - 743E (状压)
大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[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 (状压)的更多相关文章
- Hongcow Buys a Deck of Cards CodeForces - 744C (状压)
大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...
- CodeForces 11D(状压DP 求图中环的个数)
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...
- Clear The Matrix CodeForces - 903F (状压)
大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...
- Pollywog CodeForces - 917C (状压)
链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
- Keyboard Purchase CodeForces - 1238E (状压)
大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. ...
- Codeforces 1215E 状压DP
题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...
- codeforces 1185G1 状压dp
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...
随机推荐
- oracle单行函数 之 数字函数
Round(数字 \ 列 [,保留小数的位数]):四舍五入 select Round(1234.45,1) from dual = 1234.5 Trunc(数字 \ 列 [,保留小数的位数] ...
- oogle advertiser api开发概述——速率限制
速率限制 为了向遍布全球的 AdWords API 用户提供可靠的服务,我们使用令牌桶算法来衡量请求数并确定每秒查询数 (QPS) 速率.这样做的目的是阻止恶意的或不可控的软件大量入侵 AdWords ...
- python时间
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import time import datetime line="Wed 11/2 ...
- org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup' from result set. Cause: java.sql.SQLException: Error
异常展示: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup ...
- HDU 5242 Game(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=5242 题意: 给出一棵树,每个节点都有一个权值,每次可以获得从根结点(1)到叶子节点上的所有权值和,每个节点只能 ...
- Linux shell 计算两个文件的并集、交集、差集
假设我们现在有两个文件 a.txt .b.txt a.txt 中的内容如下: a c 1 3 d 4 b.txt 中的内容如下: a b e 2 1 5 # Example 01 计算并集: [roo ...
- urllib模块中的方法
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...
- _itemmod_enchant_groups
随机附魔组 附魔组 `groupId` 分组编号,同一groupId的附魔效果被随机抽取 `enchantId` 附魔Id 对应SpellItemEnchantment.dbc `chance` 被抽 ...
- Easyui使用心得(1)--DateGrid表格
最近一直在用easyui这个控件,有一点心得,在这里和大家分享一下,也是对自己工作的一个小小的总结,希望可以形成一个完整的Easyui的笔记体系,可以方便更多的人 因为自己也是在摸索中前进,难免有遗漏 ...
- Python安装常见问题:ModuleNotFoundError: No module named '_ctypes' 解决办法
一般位于3.7以上版本编译安装时出错 缺少依赖包libffi-devel 在安装3.7以上版本时,需要一个新的libffi-devel包做依赖 解决方法: yum install libffi-dev ...