[ONTAK2015]OR-XOR

题目大意:

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

思路:

首先求出前缀异或和,答案相当于选出\(m\)个数\(B_{1\sim m}\),使得\(\vee_{i=1}^{n}B_i\oplus B_{i-1}\)最小。其中\(B_m=A_n\),\(B_0=0\)。

从高到低按位贪心,某一位可以为\(0\),当且仅当这\(A_n\)一位为\(0\),且这一位为\(0\)的数的个数超过\(m\)。如果这一位确实可以为\(0\),那么就把这一位为\(1\)的数删掉。

源代码:

#include<cstdio>
#include<cctype>
typedef long long int64;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e5+1;
int64 a[N];
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) {
a[i]=a[i-1]^getint();
}
int64 ans=0;
for(register int i=63;i>=0;i--) {
if(a[n]>>i&1) {
ans|=1ll<<i;
continue;
}
int cnt=0;
for(register int j=1;j<=n;j++) {
if(~a[j]) cnt+=!(a[j]>>i&1);
}
if(cnt<m) {
ans|=1ll<<i;
continue;
} else {
for(register int j=1;j<=n;j++) {
if((~a[j])&&(a[j]>>i&1)) a[j]=-1;
}
}
}
printf("%lld\n",ans);
return 0;
}

[ONTAK2015]OR-XOR的更多相关文章

  1. BZOJ 4245: [ONTAK2015]OR-XOR

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 492  Solved: 269[Submit][Sta ...

  2. BZOJ4245 ONTAK2015 OR-XOR 【位运算+贪心】*

    BZOJ4245 ONTAK2015 OR-XOR Description 给定一个长度为n的序列a[1],a[2],…,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的 ...

  3. 【BZOJ4245】[ONTAK2015]OR-XOR 贪心

    [BZOJ4245][ONTAK2015]OR-XOR Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所 ...

  4. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  5. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. BZOJ 2115 【Wc2011】 Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  7. xor和gates的专杀脚本

    前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...

  8. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  9. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  10. BC之Claris and XOR

    http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. 启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space

    三种因素引起: 1.jvm(jdk)的内存引起. 2. eclipse的内存引起. 3.tomcat的内存引起. 1.解决方法: 2.解决方法: 解决问题的方式就是:修改了安装目录eclipse.in ...

  2. springboot拦截器HandlerInterceptor详解

    Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但 ...

  3. h5在手机端实现简单复制

    <a href="https://blog-static.cnblogs.com/files/ruanqin/clipboard.min.js">下载clipborrd ...

  4. SqlBulkCopy批量插入数据神器

    1.简单例子 class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); DataTable d ...

  5. BeautifulSoup下Unicode乱码解决

    今天在用scrapy爬某个网站的数据,其中DOM解析我用的是BeautifulSoup,速度上没有XPath来得快,不过因为用了习惯了,所以一直用的bs,版本是bs4 不过在爬取过程中遇到了一些问题, ...

  6. jenkins X实践系列(4) —— jenkins X 构建提速

    jx是云原生CICD,devops的一个最佳实践之一,目前在快速的发展成熟中.最近调研了JX,这里为第4篇,介绍如何加入jx构建和部署. builder镜像下载慢 先在一台机器上下载好,然后放到本地仓 ...

  7. iframe获取元素

    原生js在网页中,父元素获取iframe中的元素: window.onload=function () { 例如: console.log(window.frames["iframe的nam ...

  8. Codeforces 1045A Last chance 网络流,线段树,线段树优化建图

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...

  9. day 34 编程之补充内容

    生产消费者模型(必须要理解并且牢记,默写内容): from multiprocessing import Process,Queue import time,random,os def procduc ...

  10. oralce不像Java,java中字符串+数字,能够得到结果字符串

    oracle得到的两个字段进行相加,要求都是number类型的,如果两个是字符串会自动转成number类型(前提是能够转) select a+b from (select '1' a,'2' b fr ...