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的路径来,然后我们可以选一些环. 其实不管这个环和这条路径有怎样的关系,我 ...
随机推荐
- linux如和对其他用户隐藏进程?
Linux kernel 3.2以上,root用户可以设置内核,让普通用户看不到其它用户的进程.适用于有多个用户使用的系统.该功能由内核提供,因此本教程适用于Debian/Ubuntu/RHEL/Ce ...
- hadoop学习笔记——环境搭建
基础环境准备: 系统:(VirtualBox) ubuntu-12.04.2-desktop-i386.iso hadoop版本:hadoop-0.20.203.0rc1.tar.gz jdk版本:j ...
- python的私有化
1.双下划线(__) 由双下划线開始的属性在执行时被混淆,所以直接訪问是不同意的.实际上以双下划线開始的属性 会在解释时在前面加上下划线和类名.如self.__num会被解析为self._classN ...
- Unity3d载入外部图片文件
unity里的图片在生成时会压缩成资源文件,有时客户想自己放一些图片用unity显示,就必须载入外部图片. 大体思路:用Application.streamingAssetsPath或Applicat ...
- angular 资源路径问题
1.templateUrl .component("noData",{ templateUrl:"components/noData.html" // 注意相对 ...
- vue2.0 自定义过滤器
2.0中已经废弃了过滤器,需要我们自定义 <div id="app"> {{message|uppercase}} </div> //过滤器 Vue.fil ...
- Solaris磁盘配额
磁盘配额:就跟汽车行驶速度一样.我们设定. 添加rq标志,使其支持配额功能. 命令:quota ,打开就是quotaon 对ZFS文件系统进行设置配额: 当设置quota=none 则可以取消配额设置 ...
- linux 下weblogic启动和停止
启动weblogic 本例中weblogic 安装路径为:/data/weblogic/wls/wlserver_10.3/ 1. 启动nodeManager cd /data/weblogic/wl ...
- C语言--函数篇
1-1.函数简单调用 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 int ...
- windows下搭建hadoopproject(一)
这里是接着之前的一篇 <hadoop在windows下的环境搭建 >来的~~~ 一.安装文件准备 1:下载好hadoop-1.0.0.tar.gz, 下载地址是https://archiv ...