CFdiv2 165E. Compatible Numbers 子集枚举
题意:
给出一个序列,输出每个数x对应的一个ans,要求ans在数列中,并且ans & x = 0;数列的每个数小于(4e6)
思路:
这道题的方向比较难想。想到了就比较轻松了,可以这样考虑,如果(11011)2的答案知道了,那么(11001)2,(11000)2等的答案其实就是那个答案。
注意 (1<<22)-1 ,所以直接从最大的向下枚举,对于每个数,看看把0位变成1位后是否能有答案。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = (<<) - ;
int a[maxn],dp[maxn];
int main(){
int n; scanf("%d", &n); memset(dp, -, sizeof(dp));
for(int i=; i<=n; i++) scanf("%d", &a[i]), dp[maxn & (~a[i])] = a[i]; for(int i=maxn; i>=; i--){
if(dp[i] > ) continue; for(int j=; j<; j++){
if((i & (<< j)) == && dp[i^(<<j)] > ){
dp[i] = dp[i^(<<j)];
break;
}
}
} for(int i=; i<=n; i++) printf("%d ", dp[a[i]]);
puts("");
return ;
}
CFdiv2 165E. Compatible Numbers 子集枚举的更多相关文章
- Codeforces 165E Compatible Numbers(二进制+逆序枚举)
E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- 【最小生成树+子集枚举】Uva1151 Buy or Build
Description 平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方. 另外还有q(0<=q< ...
- UVA11825 黑客的攻击 Hackers' Crackdown 状压DP,二进制,子集枚举
题目链接Click Here [题目描述] 假如你是一个黑客,侵入了一个有着\(n\)台计算机(编号为\(1.2.3....n\))的网络.一共有\(n\)种服务,每台计算机都运行着所有服务.对于每台 ...
- Codeforces 165 E. Compatible Numbers【子集前缀和】
LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
- UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)
Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...
- BZOJ 1688: Disease Manangement (子集枚举)
Disease Manangement Q - 枚举子集 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- uva 11825 Hackers' Crackdown (状压dp,子集枚举)
题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...
- Sheldon Numbers GYM -- 枚举
Sheldon Numbers GYM 题意:定义Sheldon Number为其二进制数是ABA……ABA型的或者ABAB……AB的,其中A全为1,B全为0(A>0, B>0),问[m, ...
随机推荐
- HelloDjango 系列教程:创建 Django 博客的数据库模型
文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 设计博客的数据库表结构 博客最主要的功能就是展示我们写的文章,它需要从某个地方获取博客文章数据才能把文章展示出来,通常来说这个 ...
- 【iOS】tableView:cellForRowAtIndexPath: 方法未调用
今天遇到这个问题, UITableView 的代理方法 tableView:cellForRowAtIndexPath: - (UITableViewCell *)tableView:(UITable ...
- 【转】解决eclipse连接不到genymotion的问题
(1)很多朋友在使用genymotion开发安卓应用程序的时候,会遇见完全正确的安装但是在运行的时候仍然找不到,genymotion上的设备,在打开的devices上找不到如下图所示: (2)解决的方 ...
- git指令-未完待更新
git指令 1. $ git config --global user.name "Your Name" $ git config --global user.email &quo ...
- 用scrapy爬取搜狗Lofter图片
用scrapy爬取搜狗Lofter图片 # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Reques ...
- (通俗易懂小白入门)网络流最大流——EK算法
网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...
- centos yum 安装 mariadb
1. 在 /etc/yum.repos.d/ 下建立 MariaDB.repo,输入内容 [mariadb] name=MariaDB baseurl=http://yum.mariadb.org/1 ...
- Flink 源码解析 —— 源码编译运行
更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...
- IPC机制1
1.Android IPC简介 Inter-Process Communication的缩写就是IPC,含义是进程间通信或是跨进程间通信,是指两个进程进行交换数据的过程. 进程是什么? 进程在pc上就 ...
- Python 命令行之旅 —— 初探 argparse
『讲解开源项目系列』启动--让对开源项目感兴趣的人不再畏惧.让开源项目的发起者不再孤单.跟着我们的文章,你会发现编程的乐趣.使用和发现参与开源项目如此简单.欢迎联系我们给我们投稿,让更多人爱上开源.贡 ...