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的路径来,然后我们可以选一些环. 其实不管这个环和这条路径有怎样的关系,我 ...
随机推荐
- c++ concurrency serial 1: introduction
platform: vs2012 Code#include <iostream> #include <thread> using namespace std; void Fun ...
- vue Syntax Error: Unexpected token {
> music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...
- 1、CRM2011编程实战——清空指定页签以下的全部选项,并对页签以下的指定控件进行操作
需求:当页面载入时,"呼叫编号"保持不变,"任务号"自己主动更新."接报时间"和"发生日期"自己主动设置为当天日期和时间 ...
- jsp中获取spring 管理的bean(通过config)
WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...
- linux遍历目录源代码
<pre code_snippet_id="1622396" snippet_file_name="blog_20160324_1_744516" nam ...
- Linux 学习之虚拟机下的网络连接
参考资料: http://wenku.baidu.com/link?url=_55RWvvBKQDoZjQSo-HQ3TdmLIzX1zkA_g1znCw0IXkwvxbxMiA3KfpyaL-lhv ...
- 【HDOJ 5371】 Hotaru's problem
[HDOJ 5371] Hotaru's problem Manacher算法+穷举/set Manacher算法一好文:http://blog.csdn.net/yzl_rex/article/de ...
- Fedora 25/24/23 nVidia Drivers Install Guide
https://www.if-not-true-then-false.com/2015/fedora-nvidia-guide/ search Most Popular Featured Linux ...
- caffe学习--使用caffe中的imagenet对自己的图片进行分类训练(超级详细版) -----linux
http://blog.csdn.net/u011244794/article/details/51565786 标签: caffeimagenet 2016-06-02 12:57 9385人阅读 ...
- Java链接MySql数据库(转)
import java.sql.*; public class JDBCTest { public static void main(String[] args){ // 驱动程序名 String d ...