[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. azkaban安装使用

    本文记录azkaban的安装和 一些报错处理(文章末尾). AzKaban组成 MySQL数据库,azkaban-server (web端),azkaban-executor (执行job) 1.下载 ...

  2. C#版本与Framework的关系

    C# 1.0 released with .NET 1.0 and VS2002 (January 2002) C# 1.2 (bizarrely enough); released with .NE ...

  3. 盘点那些Vs中常用到的Tab快捷编码

    1.快速声明for循环:for+Tab 2.快速声明Foreach遍历:foreach+Tab 3.快速定义属性:prop+Tab 4.

  4. 查询Linux系统中glibc的版本

    编写一个简单的程序 #include <stdio.h> int main() { printf("Hello world\n"); ; } 编译 gcc test.c ...

  5. POJ3074 Sudoku 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解. 题解 DLX + 矩阵构建  (两个传送门) 代码 #include & ...

  6. netty01(长短连接、java)

    使用netty需要添加依赖包 netty版本:netty-5.0.0.Alpha2 http://files.cnblogs.com/files/applerosa/netty-5.0.0.Alpha ...

  7. 045 Java中数据导入到excel

    程序有些参考,不过重要的是思路. 1.导入依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.p ...

  8. LeetCode 237. 删除链表中的节点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. MySQL查询本周、上周、本月、上个月份数据的sql脚本

    2018-11-13 查询今天的数据 select * from 表名 where to_days(时间字段名) = to_days(now()); 查询昨天的数据 SELECT * FROM 表名 ...

  10. TensorFlow池化层-函数

    池化层的作用如下-引用<TensorFlow实践>: 池化层的作用是减少过拟合,并通过减小输入的尺寸来提高性能.他们可以用来对输入进行降采样,但会为后续层保留重要的信息.只使用tf.nn. ...