[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. Java利用POI读取Excel

    官网直接下载POI  http://poi.apache.org/ package com.CommonUtil; import java.io.File; import java.io.FileIn ...

  2. 如何设置Navicat的显示字体与字体大小?

    方法/步骤     打开Navicat   点击[工具]菜单,再选择[选项]   在[选项]界面,点击[外观]下的[字体]   设置网格字体和大小   设置编辑器字体和大小   设置命令列界面字体和大 ...

  3. Java中堆内存和栈内存的区别

    Java把内存分成两种,一种叫做栈内存,一种叫做堆内存. 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空 ...

  4. JDK 自带压缩解压流

    代码如下 package com.test.java.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

  5. [转] Web前端开发工程师常用技术网站整理

    1.常用工具相关 有道云笔记 http://note.youdao.com/signIn/index.html 36镇-最好用的共享收藏夹 http://www.36zhen.com/ 浏览器同步测试 ...

  6. [转] 对express中next函数的一些理解

    最近公司在使用node做前后端分离,采用的web框架是express,所以对express框架进行了深入的了解,前段时间写了篇关于express路由的文章,但是在那篇文章中貌似少了一个很重要的内容,就 ...

  7. MySQL应用异常问题解决

    MySQL错误:Every derived table must have its own alias 派生表都必须有自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时候子查询出来的 ...

  8. npm报错没有权限

    在npm install经常会报错没有权限 这个时候需要清除一下缓存 npm cache clean --force

  9. Mysql my.cnf配置文件记录

    一.参数 1.max_binlog_size = 1G      #binlog大小 2. #slave不需要同步数据库 binlog-ignore-db=information_schema bin ...

  10. BZOJ2527 [Poi2011]Meteors 整体二分 树状数组

    原文链接http://www.cnblogs.com/zhouzhendong/p/8686460.html 题目传送门 - BZOJ2527 题意 有$n$个国家. 太空里有$m$个太空站排成一个圆 ...