[CSAcademy]Or Problem

题目大意:

一个长度为\(n(n\le2\times10^5)\)的序列\(A(0\le A_i<2^{20})\),将其分为恰好\(m\)个连续段,设每一段的代价为这一段数字的或,总代价为每一段代价和。求最小代价和。

思路:

一个普通的DP思路是,对于每个数\(A_i\),枚举每一位,找到上一个在这一位上为\(1\)的数\(A_k\),\(A_{k+1\sim i}\)为最后一段。转移方程为\(f[i][j]=\max\{f[k][j-1]+\vee_{\ell=k+1}^i A_{\ell}\}\)。

使用带权二分可以去掉\(m\)段的状态。

源代码:

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=2e5+1,logN=18,logA=20;
int n,st[N][logN],p[logA],g[N];
int64 f[N];
inline int lg2(const float &x) {
return ((unsigned&)x>>23&255)-127;
}
inline int calc(const int &l,const int &r) {
const int k=lg2(r-l+1);
return st[l][k]|st[r-(1<<k)+1][k];
}
inline void solve(const int &c) {
memset(p,0,sizeof p);
for(register int i=1;i<=n;i++) {
f[i]=calc(1,i)-c;
g[i]=0;
for(register int j=0;j<logA;j++) {
if(st[i][0]>>j&1) p[j]=i;
if(!p[j]) continue;
const int64 tmp=f[p[j]-1]+calc(p[j],i)-c;
if(f[i]<tmp) {
f[i]=tmp;
g[i]=0;
}
if(tmp==f[i]) g[i]=std::max(g[i],g[p[j]-1]+1);
}
}
}
int main() {
n=getint();
const int m=getint();
for(register int i=1;i<=n;i++) st[i][0]=getint();
for(register int j=1;j<logN;j++) {
for(register int i=1;i+(1<<(j-1))<=n;i++) {
st[i][j]=st[i][j-1]|st[i+(1<<(j-1))][j-1];
}
}
int l=0,r=1e9;
int64 ans=0;
while(l<=r) {
const int mid=(l+r)>>1;
solve(mid);
if(g[n]>=m) {
l=mid+1;
ans=f[n]+1ll*m*mid;
} else {
r=mid-1;
}
}
printf("%lld\n",ans);
return 0;
}

[CSAcademy]Or Problem的更多相关文章

  1. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

  2. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  3. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  4. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  5. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  6. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  7. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

随机推荐

  1. AI学习吧-登录注册

    登录注册注销 如果需要给表设置权限,没有登录就不可以查看,只需要在每个视图函数之前加上Auth_classes=[ ]即可! 增加两张表,做登录认证 #models.py #做登录验证 class U ...

  2. 在 Windows服务器中启用/禁用SMBv1、SMBv2和SMBv3的方法

    本文介绍如何在 SMB 客户端和服务器组件上启用/禁用服务器消息块 SMBv1.SMBv2 和 SMBv3. 注意:建议由专业技术工程师完成以下操作. 禁用 SMBv2 和 SMBv3 的影响 我们建 ...

  3. 一脸懵逼学习KafKa集群的安装搭建--(一种高吞吐量的分布式发布订阅消息系统)

    kafka的前言知识: :Kafka是什么? 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算.kafka是一个生产-消费模型. Producer:生产者,只负责数 ...

  4. elk服务器和运维服务器的IPTABLES

    --运维服务器 iptables -P INPUT ACCEPT iptables -F iptables -X iptables -Z iptables -A INPUT -i lo -j ACCE ...

  5. [转] Javascript 原型链

    1. 类 在C或者Java里,int a;定义了一个int类型的变量a.其中int是类型的名字,a是具体的变量. Javascript 模仿自 Java, 有一部分面向对象编程的部分.在面向对象的编程 ...

  6. BFC的形成和排版规则

    何为bfc? BFC(Block Formatting Context)直译为“块级格式化范围”.是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和 ...

  7. pycharm创建python模板文件

    1.新建一个项目: 2.右键单击项目名称-->选择新建-->编辑模板文件 3.编辑模板文件保存 4.新建文件测试 至此不再重复添加头部信息了

  8. python全栈开发day51-jquery插件、@media媒体查询、移动端单位、Bootstrap框架

    一.昨日内容回顾 技术行业 (1)ajax技术 XMLHttpRequest() <1>创建XMLHttpRequest()对象 <2>检测状态(通过readyState的改变 ...

  9. 聊聊Docker数据卷和数据卷容器

    当程序在容器运行的时候,特别是需要与其他容器中的程序或容器外部程序进行沟通交流,这时需要进行数据交换,作为常用的两种沟通数据的方式,网络通信与文件读写是需要提供给程序的支持, [数据卷] 文件是数据持 ...

  10. ELK+Redis+Nginx服务数据存储以及Nginx日志的收集

    PS:此片文章是承接上篇ELK部署文档,再次便不详细说明了 [安装Redis] [root@Redis ~]# wget  http://download.redis.io/releases/redi ...