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\)颗 ...
随机推荐
- js 字符串转对象
使用eval var test = '{ colkey: "col", colsinfo: "NameList" }' var obj2 = eval(&quo ...
- spring-AOP框架(基于配置文件的方式配置AOP)
.xml: ref-指向,order-指定优先级
- 简单说说 Java 的 JVM 内存结构
问:简单说说 Java 的 JVM 内存结构分为哪几个部分? 答:JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分,分别解释如下.虚拟机栈:线程私有的,每个方法在执行时会创建一个 ...
- 通过IOCTL_ATA_PASS_THROUGH访问ATA设备接口
控制代码功能:像ATA硬盘发送ATA指令.IDE/ATA:接口,一个串行,一个并行,一般叫做IDE接口的硬盘和ATA接口的硬盘.ATA指令:可以操作ATA硬盘的指令. typedef struct _ ...
- 关于div
<article id="a"> <div class="a1"></div> <div class="a2 ...
- sp_who, sp_who2和sp_who3
sp_who可以返回如下信息: (可选参数LoginName, 或active代表活动会话数)Spid (系统进程ID)status (进程状态)loginame (用户登 ...
- ubuntu14 16使用libusb过程中遇到的问题及解决方法
从ubuntu16换到ubuntu14后安装libusb运行一直在libusb_bulk_transfer语句出现运行出现段错误,分别换了libusb1.0.0,1.0.9及1.0.21. 通过查阅链 ...
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
- 通过分析Ajax请求 抓取今日头条街拍图集
代码: import os import re import json import time from hashlib import md5 from multiprocessing import ...
- HTML5绘制饼图示例(一)
原文地址:http://www.2cto.com/kf/201108/100251.html HTML5引入Canvas元素,用于图形的绘制,我们可以仅仅基于HTML和JavaScript就能绘制出原 ...