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. 配置gosublime

    Installation Sublime Package Control allows you to easily install or remove GoSublime (and many othe ...

  2. C++之强制类型转换

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  3. 配置使用TargetFrameworks输出多版本类库

    1.类库右键 2.修改配置 修改前: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Targe ...

  4. Git------pull出错解决方法

    1.图片 转载:http://www.2cto.com/os/201404/293183.html

  5. 调用组件的C++代码

    #include<stdio.h>#include "LJSummary.h"#include<iostream>int main(void){ print ...

  6. kotlin正式由Goole公布为Android的最新开发语言

    那么,现在大家开发Android的话一般来说都是直接用Java,这个没错吧(高手除外).嗯,那么用力那么久的Java,不知道大家是否有想过Java的不足,已经很多可以优化的地方呢.当然,新修订的版本中 ...

  7. C语言基础之水仙花数

    题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身. 例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方. 程序分析:利用for循 ...

  8. LAMP集群项目五 项目备份

    1.打包到本地 2.推送到备份服务器 3.删除若干天前的备份 ip=`awk '/IPADDR/' /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F ...

  9. shell命令发送网站请求

    GET请求:curl "http://192.168.87.195:8888/refresh" POST请求:curl -d "name=value" &quo ...

  10. Hadoop入门必须知道的简单知识

    Hadoop入门知识 Hadoop构成 Hadoop由4个主要构成部分: 1) 基础核心:提供基础的通用的功能 2) HDFS:分布式存储 3) MapReduce:分布式计算 4) YARN:资源分 ...