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. shell脚本学习总结11--脚本调试

    参数: -n    不执行脚本,仅检查语法是否错误 -v    将脚本内容输出到屏幕上,然后执行脚本 -x   执行脚本,并将内容输出到屏幕 -n [root@new sbin]# sh -n deb ...

  2. Java四种会话跟踪技术

    1.cookie:2.session:3.隐藏表单域:4.URL重写

  3. LA5059 Playing With Stones

    题意:nim游戏.加上限制每次不得取走超过当前堆一半的石子 1 ≤ N ≤ 100,1 ≤ ai ≤ 2 ∗ 1018 分析:由于ai过大.所以我们采用SG函数递推找规律. (详见代码) #inclu ...

  4. 【BZOJ4881】5月月赛D 线段游戏 树状数组+set

    Description quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐 标分别为(0,i)和(1,p_i),其中p_1,p_2,. ...

  5. SQL使用union合并查询结果(转载)

    1.UNION的作用  UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...

  6. 170417、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    前言:互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服 ...

  7. Struts文件下载(静态)

    前言:在实际的开发中,我们可能需要动态下载和静态下载, 动态下载:例如我上传了一个文件,你想下载,那就必须动态生成下载链接,因为我服务器一开始是不知道我上传的东西是什么,放在那里, 静态下载:比如一些 ...

  8. R语言中将数据框(data.frame)中字符型数据转化为数值型

    as.data.frame(lapply(data,as.numeric))

  9. protobuf在java应用中通过反射动态创建对象(DynamicMessage)

    ---恢复内容开始--- 最近编写一个游戏用到protobuf数据格式进行前后台传输,苦于protobuf接受客户端的数据时是需要数据类型的如xxx.parseForm(...),这样就要求服务器在接 ...

  10. netty 网关 http请求 请求转发

    https://netty.io/4.1/xref/io/netty/example/proxy/package-summary.html https://netty.io/4.1/xref/io/n ...