[CSAcademy]Or Problem
[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的更多相关文章
- Codeforces 845G Shortest Path Problem?
http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
随机推荐
- 抓包工具Charles Proxy v4.1.1破解版下载
移动开发抓包工具Charles Proxy破解版下载 下载Charles Proxy版本,http://charles.iiilab.com/或 https://www.charlesproxy.co ...
- 饮冰三年-人工智能-Python-16Python基础之迭代器、生成器、装饰器
一:迭代器: 最大的特点:节省内存 1.1 迭代器协议 a:对象必须提供一个next方法, b:执行方法要么返回迭代中的下一项,要么抛弃一个Stopiteration异常, c:只能向后不能向前. 1 ...
- 插件使用一颜色选择器---cxColor
cxColor 是一款颜色选择器.这样的插件使用场景不多.可喜的这是国人写的. 官方网站: https://github.com/ciaoca/cxColor 使用方法: 1.引入jquery库 1 ...
- know yourself
Know yourself! ------Socrates 麦穗 古希腊有一位大学者,名叫苏格拉底.一天,他带领几个弟子来到一块麦地边.那正是收获的季节,地里满是沉甸甸的麦穗.苏格拉底对弟子们说:“你 ...
- 爬取文件时,对已经操作过的URL进行过滤
爬取文件时,对已经操作过的URL进行过滤 1.创建过滤规则文件filter.py在spiders同级目录 class RepeatUrl: def __init__(self): self.visit ...
- Win10 配置Tomcat与Java环境变量
一:下载JKD与Tomcat包 JDK 密码:d9ym Tomcat 密码:z9pa 二:安装JAVA-JDK与配置环境变量 ①:记住安装的地址 ②:配置JAVA-JDK的环境变量, ...
- webpack学习笔记--多种配置类型
除了通过导出一个 Object 来描述 Webpack 所需的配置外,还有其它更灵活的方式,以简化不同场景的配置. 下面来一一介绍它们. 导出一个 Function 在大多数时候你需要从同一份源代码中 ...
- (原创)C# 压缩解压那些事儿
吐槽: 搜狗推广API的报告服务太坑爹了!!! 搜狗推广API的报告服务太坑爹了!!! 搜狗推广API的报告服务太坑爹了!!! 搜狗的太垃圾了,获取下来的压缩包使用正常方式无法解压!!没有专门的API ...
- poshytip基本使用
js基本调用方法 $("#tips").poshytip({ content: $this.text(), alignTo: 'target', alignX: direction ...
- 背包的一些idea
题解: 给出n个物品,每次能使用l-r之间的物品,问能不能表示出k,m次询问 k<=100,m,n=1e5 想了线段树分治 发现是k^2(n+m)logn claris告诉我可以直接分治 我们对 ...