ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

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;
}

  

时间: 2024-10-24 22:09:19

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

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

hdu 5014 Number Sequence(贪心)

题目链接:hdu 5014 Number Sequence 题目大意:给定n,表示有0~n这n+1个数组成的序列a,要求构造一个序列b,同样是由0~n组成,要求∑ai⊕bi尽量大. 解题思路:贪心构造,对于n来说,找到n对应二进制的取反对应的数x,那么从x~n之间的数即可两两对应,然后x-1即是一个子问题. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

HDU 5014 Number Sequence(贪心)

当时想到了贪心,但是不知为何举出了反列....我是逗比,看了点击打开链接.才发现我是逗比. Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the integratin

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

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

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

hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB   描述 There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can pile up any amount of boxes. The limitation is that you can only pile a

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

HDU 5014 Number Sequence(西安网络赛H题)

HDU 5014 Number Sequence 题目链接 思路:对于0-n,尽量不让二进制中的1互相消掉就是最优的,那么只要两个数只要互补就可以了,这样每次从最大的数字,可以找到和他互补的数字,然后这个区间就能确定了,然后剩下的递归下去为一个子问题去解决 代码: #include <cstdio> #include <cstring> const int N = 100005; int n, a[N], ans[N]; int cnt[N]; int count(int x) {