zoukankan      html  css  js  c++  java
  • hdu 5014 异或序列

    http://acm.hdu.edu.cn/showproblem.php?pid=5014

    从最大的一个数开始找能配对使他们的异或值最大的一个数

    最后输出

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define RD(x) scanf("%d",&x)
    #define RD2(x,y) scanf("%d%d",&x,&y)
    #define clr0(x) memset(x,0,sizeof(x))
    typedef long long LL;
    
    int s[100005],tran[100005];
    bool vis[100005];
    int tr(int x)
    {
        int cnt = 0;
        while(x){
            cnt++;
            x>>=1;
        }
        return (1<<cnt) - 1;
    }
    int main(){
        int n;
        while(~scanf("%d",&n)){
            for(int i=0;i<=n;i++){
                scanf("%d",&s[i]);
            }
            clr0(vis);
            LL ans = 0;
            for(int x = n;x >= 0;--x){
                if(!vis[x]){
                    vis[x] = true;
                    int now = 1LL * tr(x);
                    int y = now^x;//cout<<x<<','<<y<<endl;
                    if(!vis[y]){
                        ans += now<<1;
                        tran[x] = y,tran[y] = x;
                        vis[y] = true;
                    }
                    else
                        ans += x,tran[x] = x;
                }
            }
            printf("%I64d
    ",ans);
            for(int x = 0;x <= n;++x)
                printf("%d%c",tran[s[x]]," 
    "[x == n]);
         }
         return 0;
     }


  • 相关阅读:
    新项目反思
    nei网访问
    react
    css沉默
    沉默
    node准备
    C++学习(八):删除文件
    利用工具格式化日期文本
    PageHelper 记录总条数不正确问题处理
    tk.mybatis 多个or条件拼接
  • 原文地址:https://www.cnblogs.com/zibaohun/p/4046780.html
Copyright © 2011-2022 走看看