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 ...
随机推荐
- linux卸载不完全
kali安装一个Nessus,但是在加载插件时候断网了,插件没下载完,后面打开也无法下载插件于是果断卸载了重装,但是无法删除配置文件,恼火了一天 完全卸载软件命令 sudo apt-get --pur ...
- sqlserver05 字符串拆分
-- 规则:将 gs-abc-aa-aa 拆分为一下字符 -- gs-abc-aa-aa -- gs-abc-aa -- gs-abc -- gs select * from dbo.f_split( ...
- Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别
istView.getCount()(实际上是 AdapterView.getCount()) 返回的是其 Adapter.getCount() 返回的值.也就是“所包含的 Item 总个数”. Li ...
- ASP.NET - 使用 Eval() 绑定数据时使用 三元运算符
ASP.NET邦定数据“<%#Eval("Sex")%>”运用三元运算符: <%#(Eval("Sex", "{0}") ...
- ASP.NET - 多文件上传,纯代码,不使用插件
解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...
- 【Demo 0007】Java基础-类扩展特性
本章学习要点: 1. 掌握static 修饰的类,方法以及变量的功能及用法; 2. 掌握代码块(静态,非静态)的作用以及注意事项: 3. 了解基本数据类 ...
- Cookie不能保存中文解决方式
在用cookie保存username的时候,发现cookie值不能存中文,报例如以下错: Control character in cookie value, consider BASE64 e ...
- u-boot的nand驱动写过程分析
从命令说起,在u-boot输入下列命令: nand write 40008000 0 20000 命令的意思是将内存0x40008000开始的部分写入nand,从nand地址0开始写,写入长度是0x2 ...
- Android手机便携式wifi的使用及无线数据传输(主要针对XP系统)
适用条件: 1.可以上网的安卓系统2.2以上的智能手机,或有便携式wifi功能的安卓智能手机 2.有无线网卡的笔记本电脑或台式机(特别是XP系统) 测试手机:中兴U930 电脑:华硕K50系列笔记本 ...
- 事务代码SE16N查看数表时可以使用命令激活编辑功能
&sap_edit命令可以激活SE16N编辑功能,维护数据表,但是是除了主键不能维护外都可以修改