Number Sequence

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Problem Description

There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules:

● a i ∈ [0,n]

● a i ≠ a j ( i ≠ j )

For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):

t = (a 0  ⊕ b 0 ) + (a 1  ⊕ b 1 ) +···+ (a n  ⊕ b n )

(sequence B should also satisfy the rules described above)

Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.

Input

There are multiple test cases. Please process till EOF.

For each case, the first line contains an integer n(1 ≤ n ≤ 10 5 ), The second line contains a 0 ,a 1 ,a 2 ,...,a n .

Output

For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b 0 ,b 1 ,b 2 ,...,b n . There is exactly one space between b i and b i+1 (0 ≤ i ≤ n - 1) . Don’t ouput any spaces after b n .

Sample Input

4 2 0 1 4 3

Sample Output

20 1 0 2 3 4

题解:

  1. 要使xor值最大,那么应该是xor的结果每一位上全部为1,例如n=3,00与11,01与10,10与01,11与00, 贪心算法,从最大的那个数匹配开始,尽量满足大的数字。

  2. i从n开始, 所以1不断左移,直到匹配的数比不比i小为止。判断某一位上是0还是1,需要用到&运算符,若i的第k位上是0,那么i&(1<<k)的值就是0。

以下是代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <queue>
#include <stack>
#include <stack>
#include <map>
using namespace std; #define F(i,s,e) for(int i = s;i<e;i++)
#define FA(i,s,e) for(int i=s;i>e;i--)
#define ss(x) scanf("%d",&x)
#define s64(x) scanf("%I64d",&x)
#define write() freopen("1.in","r",stdin)
#define W(x) while(x) typedef long long LL;
LL a[100005];
LL b[100005]; int main(){
write();
LL sum,n;
W(s64(n)!=EOF){
sum=0;
for(LL i=0;i<=n;i++)s64(a[i]);
for(LL i=0;i<=n;i++)b[i]=-1;
for(LL i=n;i>=0;i--){
if(b[i]!=-1)continue;
LL t=0,k=0;
W(1){
if((i&(1<<k))==0)t+=1<<k;//依次按位取反
if(t>=i){ //直到比i大
t-=1<<k;
break;
}
k++;
}
b[i]=t;
b[t]=i;
sum+=(t^i)*2;
}
printf("%I64d\n",sum);
for(LL i=0;i<n;i++)
printf("%I64d ",b[a[i]]);//输出对应位置的值
printf("%I64d\n",b[a[n]]);
}
}

  

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据的更多相关文章

  1. Number Sequence HDU - 5014

    There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...

  2. BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...

  3. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. (KMP 模板)Number Sequence -- Hdu -- 1711

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. Winter-1-F Number Sequence 解题报告及测试数据

    Time Limit:1000MS     Memory Limit:32768KB Description ​A number sequence is defined as follows:f(1) ...

  9. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

随机推荐

  1. hdu 3905(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3905 思路:dp[i][j]表示前i分钟,睡了j分钟收获的的最大价值,并记tmp_dp[i][j]为从 ...

  2. 剑指 offer set 20 打印出和为 s 的连续正序序列

    题目 100 可以由 9~16, 或者 18 ~ 22 组成 思路 1. 与 Leetcode Container With Most Water 有些类似, 依然是平移题目. 但这道更加复杂 2. ...

  3. skeleton

    响应式模版 伯乐在线:http://hao.jobbole.com/skeleton/ cdn  : https://cdn.bootcss.com/skeleton/2.0.4/skeleton.c ...

  4. centos-iptables-scripts

    author:headsen chen date:2018-06-04  11:20:38 notice:This  article is created by headsen chen himsel ...

  5. nat123动态域名解析软件使用教程

    nat123动态域名解析软件使用教程

  6. datatables如何把列设置成hidden隐藏域?

    官网:https://datatables.net/reference/option/设置: visible: false如下: <!DOCTYPE html><html>&l ...

  7. 160427、CSS3实战笔记--多列布局

    通过阅读和学习书籍<CSS3实战>总结 <CSS3实战>/成林著.—北京机械工业出版社2011.5   多列布局适合纯文字版式设计,如报纸内和杂志类网页布局,不适合做网页结构布 ...

  8. 巨蟒python全栈开发-第11阶段 devops-git&&openpyxl2

    大纲 1.git分支 2.git tag 3.git 忽略文件 4.正则表达式 5.openpyxl写数据 6.openpyxl读数据 1.git分支 2.git tag 3.git 忽略文件 4.正 ...

  9. Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy

    NGINX Docs | NGINX Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ ...

  10. super()函数的用法

    http://www.runoob.com/python/python-func-super.html class FooParent(object): def __init__(self): sel ...