codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
来源:互联网 发布:黑色沙漠男法捏脸数据 编辑:程序博客网 时间:2024/06/10 18:15
题目链接:
codeforces 276D
题目大意:
给出l和r,用l和r之间的两个数取亦或,求得到的结果最大是多少。
题目分析:
- 首先我们想,为了让结果最大,肯定式尽量让高的位出现1,因为高的位出现1的情况是大于当前位是0且后面所有位都是1的情况的,那么我们就可以贪心的知道肯定是保证高位上的1先出现。
- 然后我们可以想,让某一位出现1,那么就是选择一个数这一位是1,一个数这一位是0,然而我们为了让最后这两个数都大于下界,所以最高位给1的那个数已经不小于下界了,所以不用管他,考虑最高位给0的那个数,为了让它也不小于下界,所以后面的位优先选1,让它超过下界,但不能超过上界。之后就是对于一个数放满之后再往另一个数里放的贪心策略,因为这样能够前一个数放满后后面全是0,一定不会超上界了。
- 最后就是万一这种贪心策略得不到解怎么办?可以选择先选择当前这个最高位放弃,同时修改左右边界,因为两个数同时选择了这一位为1或0,然后继续贪心后面的位,知道找到解为止。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long long LL;LL l,r,ans;int main ( ){ while ( ~scanf ( "%lld%lld" , &l , &r ) ) { ans = 0; LL num = 1; while ( num <= r ) num *= 2LL; num /= 2; LL num1,num2,x; while ( num ) { x = num; num1 = num2 = 0; while ( num ) { if ( num > r ) { num >>= 1; continue; } if ( num1 >= l && num2 >= l ) { if ( num1 + num <= r ) { num1 += num; ans += num; } else if ( num2 + num <= r ) { num2 += num; ans += num; } } else if ( num1 >= l ) { num2 += num; ans += num; } else { num1 += num; ans += num; } num >>= 1; } if ( num1 >= l && num2 >= l ) break; else ans = 0; l -= x; r -= x; while ( x > r ) x /= 2; num = x; } printf ( "%lld\n" , ans ); }}
1 0
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- Codeforces 276D Little Girl and Maximum XOR (贪心)
- Codeforces 276D Little Girl and Maximum XOR 贪心+构造
- Codeforces 276D Little Girl and Maximum XOR【贪心】
- D. Little Girl and Maximum XOR(贪心)
- Codeforces 276D. Little Girl and Maximum XOR
- Codeforces 276D. Little Girl and Maximum XOR(模拟)
- [CodeForces 276D] Little Girl and Maximum XOR (构造+脑洞)
- CodeForces 276 D.Little Girl and Maximum XOR(位运算)
- codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】
- 【Codeforces Round 169 (Div 2) D】【简单数位贪心】Little Girl and Maximum XOR 区间选两数使得异或值尽可能大
- Codeforces 276C Little Girl and Maximum Sum【贪心】
- CodeForces 276C Little Girl and Maximum Sum
- Codeforces 276C Little Girl and Maximum Sum
- codeforces 276 C. Little Girl and Maximum Sum
- Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum(简单贪心)
- codeforces 510D D. Fox And Jumping(dp+数论)
- codeforces 510D D. Fox And Jumping(dp+数论)
- css 盒模型详解
- IOS开发测试之单元测试一
- Handler详解之三
- 关于BufferReader中readLine的问题!小心!
- Android序列化之Parcel
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- session的生命周期
- 少年啊,来一发昆特吗(二)——昆特牌的触摸
- Jsp中的九大内置对象和四大作用域
- C++中内存对齐
- GCD的同异步、串并行以及队列死锁的问题
- zookeeper系列:zookeeper使用和原理探究
- 栈的应用之括号的匹配Java语言描述
- EditText组件drawableLeft属性设置的图片和hint设置的文字之间的距离