hdu 5014 Number Sequence(意淫题)

Number Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 545    Accepted Submission(s): 258

Special Judge

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 integrating degree t is defined as follows(“⊕” denotes exclusive or):

t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)

(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 ≤ 105), The second line contains a0,a1,a2,...,an.

Output

For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b0,b1,b2,...,bn. There is exactly one space between bi and bi+1(0
≤ i ≤ n - 1)
. Don’t ouput any spaces after bn.

Sample Input

4
2 0 1 4 3

Sample Output

20
1 0 2 3 4

Source

2014 ACM/ICPC Asia Regional Xi‘an Online

题解及题解:

瞎搞就搞出来了,最后的结果是n*(n+1),手算一下前5个就能看出来。剩下的输出从大开始向下遍历,输出2^(len)-1-i就可以了,边计算边标记(len为当前数的2进制表示形式的位数)。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int len(int n)
{
    int ans=0;
    while(n)
    {
        n>>=1;
        ans++;
    }
    return ans;
}

int s[100010];
int t[100010];
int main()
{
    int n,le,v;
    __int64 d;
    while(scanf("%d",&n)!=EOF)
    {
        memset(s,-1,sizeof(s[0])*(n+5));
        for(int i=0;i<=n;i++)
        {
            scanf("%d",&t[i]);
        }

        for(int i=n;i>=0;i--)
        if(s[i]<0)
        {
            le=len(i);
            v=1<<le;
            v--;
            s[i]=v-i;
            s[v-i]=i;
        }
        __int64 m=(__int64)n;
        d=m*(m+1);
        printf("%I64d\n",d);
        for(int i=0;i<n;i++)
        {
            printf("%d ",s[t[i]]);
        }
        printf("%d\n",s[t[n]]);
    }
    return 0;
}
时间: 2024-10-24 22:09:21

hdu 5014 Number Sequence(意淫题)的相关文章

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) {

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

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 贪心 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 ( 构造 ) 题目意思: 给出一个数列 由0 - n 组成 然后需要构造一个数列 (也是由0-n组成),使得 sum(A[i] ^ B[i])的值最大. 分析: 异或不能出现比这个数大的情况,,所以要从大的数往前找. 现计算出当前判断数的二进制位的位数m,然后再与2^m - 1进行异或,这样会保证最大,具体为什么,自己可以想一想 比如: 5 - 101 -- 3位 - 对应 111 101 ^ 111 = 010 代码: #include <cs

HDU 5014 Number Sequence(异或 进制问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 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 inte

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 integrating degree t is defined as follows("⊕" deno

hdu 1711 Number Sequence KMP 基础题

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11691    Accepted Submission(s): 5336 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],

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