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,b2,...,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
 
 

这个题目想到贪心策略就好办了。

最先想到的一种策略就是对于数k的二进制形式,把0换成1, 1换成0,亦或后将得到全1的最大数。而且每个数的0、1组合都是唯一的,自然其对应的那个数也是唯一的。

但是会遇到一种情况,比如二进制1111和11111,它们都是和0亦或得到最大。

于是这样考虑:

对于任意一个数,只有两种情况,一是和比它小的数亦或,二是和比它大的数亦或。如果和比它小的数亦或,自然对应的数第一位肯定是0。后面可能会跟若干个0,最坏是1111这种,对应的是0;如果和比它大的数亦或,会发现,对应的数二进制位数一定比这个数长,自然1111对应的可能是10000、110000等等。

于是,只需要从大到小进行一一匹配,就不会出现之前的情况。例如之前那种情况,如果11111先匹配,11111将会和0匹配,而1111将会在之前就和10000匹配了。

ps:据说sum结果是n*(n+1),可以找规律找出来。不过我暂时推不出来。

ps:结果可能很大,需要long long来存。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std; int a[100005], Hash[100005], n;
long long sum; void qt()
{
sum = 0;
memset(Hash, -1, sizeof(Hash));
int k, v, val, p;
for (int i = n; i >= 0; i--)
{
if (Hash[i] >= 0)
continue;
k = i;
p = 1;
val = 0;
for (;k;)
{
v = k & 1;
val += (v^1) * p;
p <<= 1;
k >>= 1;
}
Hash[i] = val;
Hash[val] = i;
}
for (int i = 0; i <= n; ++i)
sum += a[i]^Hash[a[i]];
} int main()
{
//freopen("test.txt", "r", stdin);
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i <= n; ++i)
scanf("%d", &a[i]);
qt();
printf("%I64d\n", sum);
for (int i = 0; i <= n; ++i)
{
if (i)
printf(" ");
printf("%d", Hash[a[i]]);
}
printf("\n");
}
return 0;
}

  

 

ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)的更多相关文章

  1. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  2. ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)

    hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB   描述 There is a strange storehouse in PKU. In this ...

  3. hdu 5011 nim博弈 (2014西安网赛E题)

    n堆石子,每次可以选一堆取走至少一个,之后你可以不操作或者把该堆石子分成两堆,每堆至少一个,和还是原来(取完石子后)的石子个数. Sample Input1121 131 2 3 Sample Out ...

  4. hdu 5007 水题 (2014西安网赛A题)

    题意:出现Apple.iPod.iPhone.iPad时输出MAI MAI MAI!,出现Sony,输出SONY DAFA IS GOOD! Sample InputApple bananaiPad ...

  5. HDU 5014 Number Sequence(位运算)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...

  6. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  7. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

随机推荐

  1. ALLEGRO修改位号回注ORCAD原理图

    资料:ALLEGRO修改位号回注ORCAD原理图 https://wenku.baidu.com/view/54e221e114791711cd7917e2.html?re=view

  2. MVC中的 @helper

    ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...

  3. 有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。

    // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...

  4. linux查看某个时间段的log

    若想在linux下查询某个时间段的log,用sed命令示例如下: $ sed -n '/2017-01-04 11:00:00/,/2017-01-04 11:20:55/p'  ejabberd.l ...

  5. 概率dp HDU 4405

    Aeroplane chess Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  6. visual studio 2010 c++ 打印 Hello world

    由于好奇心驱使温习下c高级简化语言语言(个人解释可能不太准确).下面用visual studio 2010 实现 HelloWord 打印 第一步:visual studio 2010 打开.文件-- ...

  7. servlet3.0 文件上传功能

    注意 jsp页面中file选择 的要有属性 name='file' package com.webserver.webservice; import java.io.File; import java ...

  8. vs2013工程技巧

    1 vs工程输出了dll和lib,分别是什么,有什么用? 当设置工程property的Project Defaults的Configuration Type为dll时,不光会生成该动态链接库的dll文 ...

  9. 504 Gateway Timeout Error 502 Bad Gateway

    总结 1. 502没有收到相应,或者收到了但不及时? cannot get a response in time 540收到了无效的响应 received an invalid response fr ...

  10. HTML 学习笔记 JQuery(事件)

    加载DOM 以浏览器加载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript中,通常使用window.onload方法,在JQuery中通常使 ...