Noi.ac #309. Mas的童年(贪心)
/*
用所谓的加法拆分操作得到 x + y = (x ^ y) + 2 * (x & y)
那么我们这两段异或相当于前缀和 + 2 * 分段使左右两块&最大
记当前前缀异或和为S, 那么我们要找到优秀的X最大化(S^X) & X
显然贪心可行, 插入的时候维护当前数字所有子集, 打个vis标记, 就能快速查询了
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define mmp make_pair
#define M 3000100
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
bool vis[M];
int n, a[M], s[M];
void insert(int x)
{
if(vis[x]) return;
vis[x] = true;
for(int i = 20; i >= 0; i--)
{
if(x & (1 << i)) insert(x ^ (1 << i));
}
}
int query(int x)
{
int ans = 0;
for(int i = 20; i >= 0; i--)
{
if(x & (1 << i)) continue;
if(vis[ans | (1 << i)]) ans |= (1 << i);
}
return ans;
}
int main()
{
n = read();
for(int i = 1; i <= n; i++)
{
a[i] = read();
s[i] = s[i - 1] ^ a[i];
}
for(int i = 1; i <= n; i++)
{
cout << s[i] + query(s[i]) * 2 << " ";
insert(s[i]);
}
return 0;
}
Noi.ac #309. Mas的童年(贪心)的更多相关文章
- noi.ac#309 Mas的童年(子集乱搞)
题意 题目链接 Sol 记\(s_i\)表示前\(i\)个数的前缀异或和,我们每次相当于要找一个\(j\)满足\(0 < j < i\)且\((s_i \oplus s_j) + s_j\ ...
- 【noi.ac】#309. Mas的童年
#309. Mas的童年 链接 分析: 求$max \{sj + (s_i \oplus s_j)\}$ 因为$a + b = a \oplus b + (a \& b) \times 2$ ...
- [NOI.AC#34]palinedrome 字符串hash+贪心
容易看出,只要从两边往中间扫描,碰到相等的就直接分割然后加入答案即可,判断相等用字符串hash #include<bits/stdc++.h> #define REP(i,a,b) for ...
- # NOI.AC省选赛 第五场T1 子集,与&最大值
NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...
- NOI.ac #31 MST DP、哈希
题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...
- NOI.AC NOIP模拟赛 第五场 游记
NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...
- NOI.AC NOIP模拟赛 第六场 游记
NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
- NOI.AC NOIP模拟赛 第一场 补记
NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...
随机推荐
- Typescript学习总结1
Typescript字符串处理 首先打开运行Typescript的编辑器 http://www.typescriptlang.org/play/index.html 1. 调用变量和方法 var my ...
- Hadoop YARN上运行MapReduce程序
(1)配置集群 (a)配置hadoop-2.7.2/etc/hadoop/yarn-env.sh 配置一下JAVA_HOME export JAVA_HOME=/home/hadoop/bigdata ...
- R3注入的四种方式
DLL注入 1.首先要获取想要注入的进程句柄(OpenProcess) 2.从要注入的进程的地址空间中分配一段内存(VirtualAllocEx) 3.往分配的内存位置写入要注入的DLL名称(Writ ...
- Linux之chown
命令功能: chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令, ...
- 转---CentOS安装Oracle数据库详细介绍及常见问题汇总
一.安装前准备 1.软件硬件要求 操作系统:CentOS 6.4(32bit)Oracle数据库版本:Oracle 10g(10201_database_linux32.zip)最小内存:1G(检查命 ...
- shell 查看去掉windons中的换行符
查看 cat -v 1.sh 替换 sed -i 's/\r//g' 1.sh
- SpringBoot集成RabbitMQ
官方说明:http://www.rabbitmq.com/getstarted.html 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ ...
- elasticsearch 口水篇(1) 安装、插件
一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...
- java中遍历实体类,获取属性名和属性值
方式一(实体类): //java中遍历实体类,获取属性名和属性值 public static void testReflect(Object model) throws Exception{ for ...
- document.location.search 的作用
document.location.search 的作用 document.location.search 比如一个URL是XXXX?g=1,那么document.location.search的值就 ...