2014 网选 5014 Number Sequence(异或)

简介:

/*
    题意:a, b两个序列,规定由[0, n]区间的数!
    求 a[i] ^ b[i] 的和最大! 
    
    思路:如果数字 n的二进制有x位, 那么一定存在一个数字m,使得n^m的所有二进制位
    都是1,也就是由x位1!这样下去的到的值就是最大值! 
    
*/ 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100005 
using namespace std;

int b[N], vis[N];


int pos[N];


int main(){
    int n;
    while(scanf("%d", &n)!=EOF){
        int x, y;
        for(int i=0; i<=n; ++i){
            scanf("%d", &x);
            pos[x]=i;
        }
        memset(vis, 0, sizeof(vis));
        long long sum=0;
        for(int i=n; i>=0; --i){
             y=x=i;
             if(vis[x]) continue;
             int tmp=1;
             while(y){
                 x^=tmp;
                 tmp<<=1;
                 y>>=1;
            }
            vis[x]=vis[i]=1;
            sum+=2*(x^i);
            b[pos[i]]=x;
            b[pos[x]]=i;
        }
        //printf("%lld\n", sum);
        cout<<sum<<endl;
        printf("%d", b[0]);
        for(int i=1; i<=n; ++i)
            printf(" %d", b[i]);
        printf("\n");
    }
    return 0;
}

目录
相关文章
|
存储 缓存 关系型数据库
Redo日志 (4)—log sequence number(六十二)
Redo日志 (4)—log sequence number(六十二)
|
Java C++
HDU-1005,Number Sequence(有规律的数学题)
HDU-1005,Number Sequence(有规律的数学题)
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
139 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
104 0
|
Java
HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27028    Accepted Submission...
1028 0
HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 115124    Accepted Su...
805 0
|
人工智能 Java BI
KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11606    Accepted Submission...
821 0