给出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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题

    C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #384(div 2)

    A 题意:有n个机场处于一直线上,可两两到达,每个机场只可能属于两家公司中的一家(用0,1表示),现在要从a机场到b机场,可任意次转机.若机场i与机场j从属同一公司,则费用为0,否则费用为1.问最小费 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 7.2WebApi2中的全局异常处理

    现在在WebApi中还没有一种简单的方式去记录或者处理全局的异常.一些未处理的异常可以处理通过异常过滤器,但有大量的异常是异常过滤器不能处理的.例如: 从控制器的构造函数引发的异常. 从消息处理程序引 ...

  2. Java类名.class和getClass()区别

    区别 类名.class叫做“类字面量”,因class是关键字, 所以类名.class编译时确定. getclass()运行时根据实际实例确定,getClass()是动态而且是final的. Strin ...

  3. T-SQL 语句的理解

    1.T-SQL中各子句在逻辑上按照以下顺序进行处理 . . . .. .ORDER BY 查询实例: SELECT EMPID, YEAR(ORDERDATE) AS ORDERYEAR, COUNT ...

  4. Ubuntu 使用phpmyadmin,报错#1146 - Table ‘phpmyadmin.pma_table_uiprefs' doesn't exist

    cd /etc/phpmyadminsudo vim config.inc.php 修改phpmyadmin的配置文件config.inc.php $cfg['Servers'][$i]['table ...

  5. 【Python】自动生成html文件查看指定目录中的所有图片

    获取本目录下的pic子目录中的所有图片(jpg,png,bmp,gif等,此处以jpg文件为例),然后生成一个image.html文件,打开该html文件即可在浏览器中查看pic子目录中的所有图片. ...

  6. struts2 拦截器

    拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...

  7. 我与A协

    大学毕业以后发现离曾经的圈子越来越远,非常怀念原来在A协和大家一起奋斗的日子,在这里写一篇文章,献给有很多美好回忆的A协,也献给渐渐远离A协的我. 首先,回顾一下我为什么会参与到A协的建设工作中来.我 ...

  8. linux 服务

    CentOS 7.x设置自定义开机启动,添加自定义系统服务 http://linux.it.net.cn/CentOS/fast/2015/0507/15184.html

  9. JAVA SSM 示例代码

    SSM 即spring+spring mvc+mybatis,开发工具IDEA 1.先看下项目结构如图: 2.主要配置文件 spring-mvc.xml <?xml version=" ...

  10. iOS开发UI篇—核心动画(基础动画)

    转自:http://www.cnblogs.com/wendingding/p/3801157.html 文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(基础动画) iOS ...