Codeforces Round #384 (Div. 2) E
给出n个数字 1-8之间 要求选出来一个子序列 使里面1-8的数字个数 极差<=1 并且相同数字必须相邻(112 可以但是121不行)求这个子序列的最长长度
一道状压dp 看不懂别人的dp思想..自己造了一个出来..
首先枚举t 意义是 当前选取子序列 每个数字最少是多少 那么这个子序列中的数字个数为t t+1
dp[i][j]当前在i位 选取数字的情况压缩成j
枚举没选过的数字 用前缀和进行二分查找
从第i位开始 求出res使在满足i-res这个区间有t或者t+1的某数字 进行转移 更新dp[res][j|x]
当t为0的时候和t>0的时候 一个是 某种数字不选也可以 一个是每种数字都会被选择 所以分开来讨论
前者就是统计出现过的数字的种数
后者 当j|x为11111111 即每种数字都选完的时候 这时候才会满足 数字个数为t或者t+1 才可以进行对ans的更新
时间复杂度 n^2 * (255) * log(n)
一道题补了两天...期间打了好久游戏...QAQ
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<queue>
#include<malloc.h>
using namespace std;
#define L long long
int n ;
int dp[1050][(1<<8) + 5];
int a[1050];
int b[(1<<8) + 5];
int sum[1050][9];
int main(){
scanf("%d",&n);
memset(sum,0,sizeof(sum));
int ans = 0;
int mj = (1<<8) - 1;
for(int i=0;i<=mj;i++){
int z = i;
b[z] = 0;
while(z){
if(z%2 == 1)b[i] ++;
z/=2;
}
}
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
for(int j=1;j<=8;j++)sum[i][j] = sum[i-1][j];
sum[i][a[i]] ++ ;
}
for(int i=1;i<=8;i++){
if(sum[n][i]>0)ans++;
}
for(int t=1;t*8<=n;t++){
memset(dp , -1 ,sizeof(dp));
dp[0][0] = 0;
for(int i=0;i<n;i++){
for(int j=0;j<=mj;j++){
if(dp[i][j] == -1)continue;
for(int k=1;k<=8;k++){
int z = (1<<(k-1));
if((z&j) == 0){
int g=(z|j);
int l = i+1;
int r = n;
int res = -1;
while(l<=r){
int mid =(l+r)/2;
if(sum[mid][k] - t > sum[i][k]){
r = mid - 1;
}
else if(sum[mid][k] - t < sum[i][k]){
l = mid + 1;
}
else {
r = mid - 1;
res = mid;
}
}
if(res != -1){
if(dp[res][g] == -1 || dp[res][g] < dp[i][j] + t){
dp[res][g] = dp[i][j] + t;
if(b[g] == 8)
ans = max(dp[res][g] , ans);
}
}
l = i+1;
r = n;
res = -1;
while(l<=r){
int mid =(l+r)/2;
if(sum[mid][k] - t - 1> sum[i][k]){
r = mid - 1;
}
else if(sum[mid][k] - t - 1< sum[i][k]){
l = mid + 1;
}
else {
r = mid - 1;
res = mid;
}
}
if(res != -1){
if(dp[res][g] == -1 || dp[res][g] < dp[i][j] + t + 1){
dp[res][g] = dp[i][j] + t + 1;
if(b[g] == 8)
ans = max(dp[res][g] , ans);
}
}
}
}
}
}
}
printf("%d\n",ans);
}
Codeforces Round #384 (Div. 2) E的更多相关文章
- Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp
E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题
C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...
- Codeforces Round #384 (Div. 2)B. Chloe and the sequence 数学
B. Chloe and the sequence 题目链接 http://codeforces.com/contest/743/problem/B 题面 Chloe, the same as Vla ...
- Codeforces Round #384 (Div. 2) A. Vladik and flights 水题
A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...
- Codeforces Round #384(div 2)
A 题意:有n个机场处于一直线上,可两两到达,每个机场只可能属于两家公司中的一家(用0,1表示),现在要从a机场到b机场,可任意次转机.若机场i与机场j从属同一公司,则费用为0,否则费用为1.问最小费 ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...
- Codeforces Round #384 (Div. 2) 734E Vladik and cards
E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #384 (Div. 2)D-Chloe and pleasant prizes
D. Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- Visual Studio常用快捷键
1. 代码自动对齐:CTRL+K+F 2. 撤销---使用组合键“Ctrl+Z”进行撤销操作 3. 反撤销---使用组合键“Ctrl+Y”进行反撤销操作 4. 使用组合键“Ctrl+J”或者使用组合键 ...
- Windows中explorer(图形壳)
explorer是Windows程序管理器或者文件资源管理器. 用于管理Windows图形壳.(桌面和文件管理.) 删除该程序会导致Windows图形界面无法使用. explorer.exe进程是微软 ...
- Java连接mysql数据库并插入中文数据显示乱码
连接数据库设置编码 jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
- jquery-简单拖拽代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【Network】Calico, Flannel, Weave and Docker Overlay Network 各种网络模型之间的区别
From the previous posts, I have analysed 4 different Docker multi-host network solutions - Calico, F ...
- Angular2 指令
1. 指令说明 Angular2 指令是构成Angular2应用程序的重要组成部分,指令主要用来对模板的标签或者元素附加一些新的特性或者功能,改变一个 DOM 元素的外观或行为,Angular2指 ...
- 【XLL 框架库函数】 TempBool/TempBool12
创建一个包含了 Boolean 类型的 TRUE 或 FALSE 的 XLOPER/XLOPER12 LPXLOPER TempBool(int b); LPXLOPER12 TempBool12(i ...
- tp框架之增删改查
控制器 <?php namespace Home\Controller; use Think\Controller; class CeShiController extends Controll ...
- 对DIP IoC DI的理解与运用
DIP,IoC,DI基本概念 依赖倒置原则(DIP,Dependency Inverse Principle):强调系统的“高层组件”不应当依赖于“底层组件”,并且不论是“高层组件”还是“底层组件”都 ...
- PDA移动POS终端系统,实现专柜或店铺的收货、零售、盘点通过无线网络直接连接总部中央数据库,实现高效安全的移动供应链管理
利用PDA移动终端,实现专柜或店铺的收货.零售.盘点等一体化操作,通过无线网络直接连接总部中央数据库,实现高效安全的移动供应链管理. · PDA订货会应用解决方案利用PDA或电脑系统,在订货会现场直接 ...