HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi'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 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

HDU坑爹爆long long,换了__int64过了。想法很简单,把两个数二进制的0和1尽量补全,优先满足大的数就可以了。不过要找到区间。

代码:

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

__int64 n, a[100010];
struct right
{
    __int64 s, r, l;
}rt[1000];
__int64 getNear(__int64 x)
{
    __int64 z = 1;
    while(x)
    {
        x >>= 1;
        z <<= 1;
    }
    return z-1;
}
int main()
{
    while(~scanf("%I64d", &n))
    {
        __int64 m = n;
        rt[0].r = m;
        rt[0].s = getNear(m);
        rt[0].l = rt[0].s-rt[0].r;
        //cout << rt[0].l << " " << rt[0].r << " " << rt[0].s << endl;
        __int64 cnt = 0;
        while(1)
        {
            m = rt[cnt].l-1;
            if(m  < 0) break;
            cnt++;
            rt[cnt].r = m;
            rt[cnt].s = getNear(m);
            rt[cnt].l = rt[cnt].s-rt[cnt].r;
            //cout << rt[cnt].l << " " << rt[cnt].r << " " << rt[cnt].s << endl;
        }

        for(__int64 i = 0; i <= n; i++)
            scanf("%I64d", &a[i]);
            //a[i] = i;
        __int64 t = 0;

        for(__int64 i = 0; i <= n; i++)
            for(__int64 j = 0; j <= cnt; j++)
            {
                if(a[i] >= rt[j].l && a[i] <= rt[j].r)
                {
                    //cout << rt[j].l << " " << rt[j].r << " " << rt[j].s << endl;
                    //printf("%d ", rt[j].s-a[i]);
                    a[i] = rt[j].s-a[i];
                    t += rt[j].s;
                    break;
                }
            }
            printf("%I64d\n", t);
            for(__int64 i = 0; i < n; i++)
                printf("%I64d ", a[i]);
            printf("%I64d\n", a[n]);
    }

    return 0;
}

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi'an Online) 题解

时间: 2024-12-26 20:32:16

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi'an Online) 题解的相关文章

hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi&#39;an Online)

Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 675    Accepted Submission(s): 237 Problem Description Trader Dogy lives in city S, which consists of n districts. There are n - 1

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 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

2014 ACM/ICPC Asia Regional Xi&#39;an Online(HDU 5007 ~ HDU 5017)

题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Sony” 就输出“SONY DAFA IS GOOD!” ,大小写敏感. 思路 : 字符串查找,水题. 1 #include <string.h> 2 #include <stdio.h> 3 #include <iostream> 4 5 using namespace st

HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi&#39;an Online)

思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’   'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就是模拟了. 注意:  ‘S’ 不是只有一个. 一个东西如果不是'P'在动的话要先判断周围有没有‘P’,有的话要先吃掉      'P'在动的时候如果一个位置周围有多个东西,都要吃掉. #include<iostream> #include<cstdio> #include<alg

2014 ACM/ICPC Asia Regional Xi&#39;an Online 233 Matrix,hdu 5015

比赛的时候若是这题过了就进前50 刚开始的时候大家的思路都以为是找规律的题目,于是再推公式,此外还发现类似于杨辉三角.于是又去套杨辉三角的通项去求. 于是TLE了无数次.(每次取范围的最大值也要3s多). 对于明显的矩阵样子,其实可以转化为矩阵的运算,每一行的转移.就是对一个转移矩阵的幂运算.然后再用快速矩阵幂即可. A: 10 0 0 1 10 1 0 1 10 1 1 1 0  0  0 1 B: 23 0 0 3 C=A^M  *B,ans=C[N] 教训:对于时间限制,即便是最大数据也要

2014 ACM/ICPC Asia Regional Xi&#39;an Online

03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为sqrt(n),问题是怎么快速找到从i开始往前2种颜色.三种.四种...o[]种的位置. 离散化之后,可以边走边记录某个数最后一个出现的位置,初始为-1,而所要求的位置就等于 if(last[a[i]]==-1) 该数没有出现过,num[i][1] = i,num[i][j+1] = num[i-1

HDU 4998 Rotate 计算几何 2014 ACM/ICPC Asia Regional Anshan Online

题意: 有一个平面放在一个二维坐标轴上 给定n个操作 (x,y) p 表示把平面绕着(x,y) 逆时针转p弧度. 最后的结果相当于平面绕着(X, Y) 逆时针旋转了P弧度. 求:X,Y,P 思路: 任取一个三角形ABC,然后根据n个操作得到A'B'C', 然后求外心. 旋转的角度就是相加..==为啥我也不大清楚,不是本弱写的. #include <cstdio> #include <algorithm> #include <iostream> #include <

HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description After eating food from Chernobyl,