CodeForces165E 位运算 贪心 + 状压dp

http://codeforces.com/problemset/problem/165/E

题意 

两个整数 x 和 y 是 兼容的,如果它们的位运算 "AND" 结果等于 0,亦即 a & b = 0 。例如,数 90 (10110102) 和 36 (1001002) 是兼容的,因为 10110102 & 1001002 = 02;而数 3 (112)和 6 (1102) 是不兼容的,因为 112 & 1102 = 102 。

给定一个整数数组 _a_1, _a_2, ..., a__n 。您的任务是判断每个数组元素:这个元素是否与给定数组中的某个其它元素兼容?如果问题的答案是肯定的,则应找出任意一个匹配的元素。

第一眼觉得用01字典树可做,但当所有值都是1的时候,每次遍历就要遍历整个字典树,很显然会T。只能考虑其他做法。

我们发现给的数据范围是400w,是可以直接用数组开下的,考虑到&运算的时候只有原数字位位1和另一个数字相同位为1的时候才会导致不匹配。

所以我们利用贪心的思想,先把能与这个数匹配的最大数直接存入数组,之后从后往前遍历,如果遇到没有匹配的数字,就考虑比他多一位1的数字有没有匹配,因为在相同位上1总为0的子集,因此这样预处理之后可直接输出,

1#include <map> 2#include <set> 3#include <ctime> 4#include <cmath> 5#include <queue> 6#include <stack> 7#include <vector> 8#include <string> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <sstream> 13#include <iostream> 14#include <algorithm> 15#include <functional> 16using namespace std; 17#define For(i, x, y) for(int i=x;i<=y;i++) 18#define _For(i, x, y) for(int i=x;i>=y;i--) 19#define Mem(f, x) memset(f,x,sizeof(f)) 20#define Sca(x) scanf("%d", &x) 21#define Sca2(x,y) scanf("%d%d",&x,&y) 22#define Scl(x) scanf("%lld",&x); 23#define Pri(x) printf("%d\n", x) 24#define Prl(x) printf("%lld\n",x); 25#define CLR(u) for(int i=0;i<=N;i++)u[i].clear(); 26#define LL long long 27#define ULL unsigned long long 28#define mp make_pair 29#define PII pair<int,int> 30#define PIL pair<int,long long> 31#define PLL pair<long long,long long> 32#define pb push_back 33#define fi first 34#define se second 35typedef vector<int> VI; 36const double eps = 1e-9; 37const int maxn = 1e6 + 10; 38const int INF = 0x3f3f3f3f; 39const int mod = 1e9 + 7; 40int N,M,tmp,K; 41int dp[1 << 23]; 42int a[maxn]; 43int main() 44{ 45 Sca(N); 46 For(i,1,N){ 47 int x; Sca(x); a[i] = x; 48 dp[x ^ ((1 << 23) - 1)] = x; 49 } 50 for(int i = (1 << 23) - 1; i >= 0 ; i --){ 51 if(!dp[i]){ 52 for(int j = 0; j < 23; j ++){ 53 if(dp[i | (1 << j)]){ 54 dp[i] = dp[i | (1 << j)]; 55 break; 56 } 57 } 58 } 59 } 60 For(i,1,N){ 61 if(dp[a[i]]) printf("%d ",dp[a[i]]); 62 else printf("-1 "); 63 } 64 #ifdef VSCode 65 system("pause"); 66 #endif 67 return 0; 68}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

FLV文件格式

1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid