SPOJ Find the max XOR value(二进制,贪心即可)
You have two integers L and R, and you are required to find the max xor value of a and b where L <= a <= R and L <= b <= R
Input
Two integers in a line. L, R <= 1e9
Output
One integer, the answer
Example
Input:
1 10 Output:
15
题意:
给定L,R,X1^X2^X3...最大异或,(L<=X1,X2,X3...<=R)。
没什么思路,上次CF就遇到这道题,我是用贪心写的,忽略pow的精度问题,可以AC。
http://codeforces.com/contest/912/problem/B
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
long long a,b,n,k,ans,c,d;
map<long long,int>mp;
vector<long long>S;
int main()
{
while(~scanf("%I64d%I64d",&n,&k)){
ans=;
for(long long i=log2(n);i>=;i--){
long long tmp=pow(,i);
if(k>){
k--;
mp[tmp]=;
S.push_back(tmp);
ans+=tmp;
}
else{
int L=S.size();
for(int j=;j<L;j++){
if(mp[S[j]]==&&S[j]+tmp<=n&&mp[S[j]+tmp]==) {
mp[S[j]]=;
mp[S[j]+tmp]=;
ans+=tmp;
S.push_back(S[j]+tmp);
break;
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}
但是仔细一想的话,得到了最大了2^n<=R,如果还可以异或一个,那么选择2^n-1就好了。(2^n)xor(2^n-1) =2^(n+1)-1。一定是最大的。
比如2^=10000, n=4,10000 xor 01111 = 11111;不可能还有不这个大的了,毕竟n=4是上界。当然只能选一个的时候,就选本身就好了。当然,为了避免卡精度问题(比如CF就hack我了),pow函数最好比较一下,这里太懒,算了。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int a,b,ans,L,R;
while(~scanf("%d%d",&L,&R)){
if(L==R) printf("%d\n",L);
else {
int a=log2(R);
a=q_pow(,a);
printf("%d\n",a+a-);
}
} return ;
}
---------------------------------------分界线---------------------------------------
妈蛋,上诉解法有误。。。。如果我选的数小于L呢?
所以不行的。比如L=10,R=15,标准答案是7。而我的答案是15。。。CF没有L限定,所以可以过。这个题不一样。
对比这里就知道了 https://vjudge.net/problem/HackerRank-maximizing-xor 还好发现了
SPOJ Find the max XOR value(二进制,贪心即可)的更多相关文章
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- [USACO]6.1.3 cow xor(二进制+Trie)
题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- XOR(二进制使用)
链接:https://www.nowcoder.com/acm/contest/116/H来源:牛客网 题目描述 Once there was a king called XOR, he had a ...
- Codeforces Round #649 (Div. 2) E. X-OR 交互 二进制 随机 期望
LINK:X-OR 本来是应该昨天晚上发的 可是昨天晚上 做这道题 写了一个分治做法 一直wa 然后查错 查不出来 心态崩了 想写对拍 发现交互库自己写不出来. 一系列sb操作 == 我都醉了. 今天 ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- [NOI2014]起床困难综合症(二进制+贪心)
题目 [NOI2014]起床困难综合症 做法 先用全\(0\)和全\(1\)去运行一下,再在满足\(m\)的限制下,贪心地从高位到低位选择即可
- Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)
E. XOR Guessing time limit per test1 second memory limit per test256 megabytes inputstandard input o ...
- [WC2011]最大XOR和路径(贪心+线性基)
题目大意:给一张无向图,求一条1-n的路径,是路径边权的异或和最小. 题解 这道题的思路很妙,首先我们可以随便找出一条从1到n的路径来,然后我们可以选一些环. 其实不管这个环和这条路径有怎样的关系,我 ...
随机推荐
- SilverLight:布局(2)GridSplitter(网格分割)垂直分割、水平分割
ylbtech-SilverLight-Layout: 布局(2)GridSplitter(网格分割)垂直分割.水平分割 A, Splitter(分割)对象之 GridSplitter(网格分割)1: ...
- 借助树莓派模拟Wimonitor并实现WiFi窃听和嗅探
Wimonitor是一款非常优秀的黑客工具,它不仅可以帮渗透测试人员省去配置虚拟机和无线网卡等一系列麻烦事,而且它的Web接口配置起来也非常的方便.实际上,它就是一款TP-Link-MR3020路由器 ...
- drag-html
<!doctype html><html><head><meta charset="UTF-8" /><title>Ca ...
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API ...
- AngularJS的Foreach循环示例
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSForeach.rar 代码: <!DOCTYPE HTML PUBLIC ...
- java性能监控工具jstat-windows
jstat Monitors Java Virtual Machine (JVM) statistics. This command is experimental and unsupported. ...
- Android加壳native实现
本例仅在Android2.3模拟器跑通过,假设要适配其它机型.请自行研究,这里不过抛砖引玉. 0x00 在Android中的Apk的加固(加壳)原理解析和实现,一文中脱壳代码都写在了java层非常ea ...
- iOS学习笔记12-网络(一)NSURLConnection
一.网络请求 在网络开发中.须要了解一些经常使用的请求方法: GET请求:get是获取数据的意思,数据以明文在URL中传递,受限于URL长度,所以数据传输量比較小. POST请求:post是向serv ...
- mysql 海量数据删除
百度知道 - mysql删除海量数据 MySQL 数据库删除大批量数据的优化 看到这儿的话,最后看下这篇文章,对于操作海量数据的sql深入分析 cnblogs - 深度分析DROP,TRU ...
- viewState详解
作者:Infinities Loop 概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对 ViewState的一些错误认识.为了达到这个目的,我决定从头到尾详细的描述一下整 ...