Codeforces 164 E Compatible Numbers
做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 。可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了。
解题思路:
本题要求两个数 a & b = 0 , 假设 a = 10010 , b 至少(指在 a 中的为 1 的位必须为 0 )是 01101 ,还能够是 00101 ,00001 。00000。就相当于你去买东西一样,先提出你的要求(必须满足)。至于其它方面都无所谓。
这样我们能够枚举 b 中的 1 。让其变为 0 ,那么,如何枚举呢 ? 一个一个的枚举是不能够的,肯定超时,我们能够统一枚举一下,就跟状态压缩更新状态一样,相当于递推。用动态规划的思想去优化它,每一个数最多仅仅变化
0 的个数。然后再用变化了的数去变化。
代码:
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT __int64
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const INT mod = 1e9 + 7 ;
const int MY = 15 ;
const int MX = (1<<22) + 5 ;
int n ;
int dp[MX] ,g[MX] ;
int main()
{
//freopen("input.txt" ,"r" ,stdin) ;
while(~scanf("%d" ,&n))
{
int S = (1<<22) - 1 ;
memset(dp ,0 ,sizeof(dp)) ;
for(int i = 0 ;i < n ; ++i)
{
scanf("%d" ,&g[i]) ;
dp[g[i]^S] = g[i] ; // g[I] 须要的还有一半
}
for(int i = S ; i >= 0 ; --i) // 枚举各种状态
{
if(!dp[i]) // 假设没有存值
{
for(int j = 0 ;j < 22 ; ++j) // 给其加入 1 让其变成有值
if(dp[i|(1<<j)])
dp[i] = dp[i|(1<<j)] ;
}
}
for(int i = 0 ;i < n ; ++i)
{
if(i) cout<<" " ;
if(dp[g[i]]) cout<<dp[g[i]] ;
else cout<<"-1" ;
}
cout<<endl ;
}
return 0 ;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 164 E Compatible Numbers的更多相关文章
- Codeforces 165 E. Compatible Numbers【子集前缀和】
LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
- Codeforces 165E Compatible Numbers(二进制+逆序枚举)
E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- 【Codeforces】CF 165 E Compatible Numbers(状压dp)
题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- Codeforces 934 A.Compatible Pair
http://codeforces.com/contest/934 A. A Compatible Pair time limit per test 1 second memory limit p ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
- CodeForces 682A Alyona and Numbers (水题)
Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...
随机推荐
- 实现长按删除QListWidget的Item
原地址:http://blog.sina.com.cn/s/blog_5c70dfc80100r99u.html 要想长按删除QListWidget的Item,必须重写鼠标事件,所以需要继承QList ...
- HTML5 Canvas中9宫格的坑
近期小鸟情人游戏上了手机qq空间,一个3岁的游戏来了她的第二春.为了能有更好的表现,我们对其进行了一次改版. 改版当中一项就是对原来的弹出框样式进行改进.将大块木板材质改成纯色(边框为圆角金属材质)样 ...
- SVM(支持向量机)(一)
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM(Support Vector Machines)系列会循序 ...
- Appium TestNg Maven Android Eclipse java简单启动实例
环境准备 Eclipse + maven + appium + TestNg 确保已经在Eclipse 上面安装maven TestNg的插件 打开Eclipse,新建一个java项目,把项目转换成m ...
- Windows串口编程
串口基础知识 http://www.cnblogs.com/menlsh/archive/2013/01/28/2880580.html DTU知识 http://blog.csdn.net/xuto ...
- JavaScript快速入门(二)——JavaScript变量
变量声明 JavaScript的变量声明分为显式声明跟隐式声明. 显式声明 即带var关键字声明,例如 var example = example; 要注意JavaScript里面声明的关键字只有fu ...
- Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源代码分析
上一章我们分析了Scene与Layer相关类的源代码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源代码. 直接看TransitionScene的定 ...
- android打包apk时混淆遇到的问题
android打包apk的时候一般会选择混淆,而在eclipse中常使用的是proguard来混淆.有很多时候引用了第三方包的时候会导致打包不成功,或者打包成功不能运行的情况. 首先看看正常的prog ...
- Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)
在Swift项目中,我们可以导入任意用Objective-C写的框架,代码库等.下面以Swift调用Objective-C编写的颜色选择器KKColorListPicker为例. 效果图如下: ...
- 状态压缩动态规划 -- 棋盘问题 POJ 1321
一个 N * N 的棋盘上面,有些格子不能放,放置 M 的棋子, 每两个棋子不能在同一行或者同一列,问有多少种放法 DFS太慢,用SCR好点点 Python 仅仅有 22 行,事实上能够更短.可是得排 ...