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. 解析IE, FireFox, Opera 浏览器支持Alpha透明的方法

    先请看如下代码: filter:alpha(opacity=50);       /* IE */  -moz-opacity:0.5;              /* Moz + FF */  op ...

  2. 转载 --iOS实用小技巧(2)-生成txt文本

    //不论是创建还是写入只需调用此段代码即可 如果文件未创建 会进行创建操作 - (void)writeToFileWithTxt:(NSString *)string{ dispatch_async( ...

  3. C++引用具体解释

    引用是C++中新出现的.有别于C语言的语法元素之中的一个. 关于引用的说明,网络上也有不少.可是总感觉云遮雾绕,让人印象不深刻. 今天我就来深入解释一下引用.并就一些常见的观点进行说明,最后附带代码演 ...

  4. JB开发之问题汇总 [jailbreak,越狱技术]

    1.升级到Mac 10.9.1,Xcode 升级到5出现的问题: 1)升级前要做的事情: ①升级/重新安装iOSOpenDev,在终端输入 xcode-select --switch (xcode_d ...

  5. SQL Server计算列

    计算列由可以使用同一表中的其他列的表达式计算得来.表达式可以是非计算列的列名.常量.函数,也可以是用一个或多个运算符连接的上述元素的任意组合.表达式不能为子查询. 例如,在 AdventureWork ...

  6. 微软笔试题-highways

    题目大意 一条单向的高速公路上有N辆车,在0时刻,每辆车分别在起点A[0],A[1]....处开始从北向南出发,每辆车有个终点B[0],B[1]....且每辆车有个限制速度 V[0],V[1]... ...

  7. 前端开发 - JavaScript - 总结

    一.JavaScript的特征 javaScript是一种web前端的描述语言,也是一种基于对象(object)和事件驱动(Event Driven)的.安全性好的脚本语言.它运行在客户端从而减轻服务 ...

  8. python中lambda使用

    一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称,而用def创建的方法是有名称的.如下: """命名的foo函数&q ...

  9. apt-get tips

    1.通过apt-get安装指定版本的软件 apt-get install <package name>=<version> 2.通过apt-cache列举所有可获取的版本 ap ...

  10. ovn-kubernetes执行流程概述

    Master部分 1.master初始化 以node name创建一个distributed logical router 创建两个load balancer用于处理east-west traffic ...