codeforces 165E 状态压缩

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

蛮巧妙的,关键是以前做的少,mark一下

View Code
#include<cstdio>
#include<cstring>
int f[5000010],a[1000010];
int main()
{
int n,i,j,k;
scanf("%d",&n);
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[a[i]^((1<<22)-1)]=a[i];//a[i]在二进制中的补集对应于a[i]
}
for(i=(1<<22)-1;i>=0;i--)
{
if(!f[i])
for(j=0;j<22;j++)
if(f[i|(1<<j)])//相当于并集
{
f[i]=f[i|(1<<j)];
break;
}
}
for(i=1;i<=n;i++)
{
if(f[a[i]]) printf("%d ",f[a[i]]);
else printf("-1 ");
}
}



posted @ 2012-03-18 01:06  Because Of You  Views(395)  Comments(0Edit  收藏  举报