您的位置:首页 > 理论基础 > 计算机网络

hdu 5014 Number Sequence(西安网络赛1008)

2014-09-15 23:04 316 查看


Number Sequence

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

Total Submission(s): 633 Accepted Submission(s): 300

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


从大往小的找,知道发现n+j的值是二进制全为1的数,则会发现这一段(n-1)+(j+1)的值都为二进制全为1的数,这一段就处理好了,接着再往前处理。

代码:
//390ms
#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn=100000+1000;
using namespace std;
int hash[20];
int a[maxn];
int b[maxn];
int main()
{
int n;
hash[0]=0;
hash[1]=1;
for(int i=1;i<19;i++)
{
hash[i+1]=(hash[i]<<1)+1;
}
while(~scanf("%d",&n))
{
for(int i=0;i<=n;i++)
scanf("%d",&a[i]);
int temp;
int sign=0;
int m=n;
while(m>=0)
{
if(m==0)//特判0
{
b[0]=0;
break;
}
for(int i=0;i<=19;i++)//找到第一个各位全为1的且比m大
{
if(m<=hash[i])
{
temp=hash[i];
break;
}
}
for(int i=m-1;i>=0;i--)
{
if(m+i==temp)//找到左边对称的点
{
sign=i;
break;
}
}
for(int i=sign;i<=m;i++)
{
b[i]=temp-i;
}
m=sign-1;
}
long long ans=0;
for(int i=0;i<=n;i++)
ans=ans+(i^b[i]);
printf("%I64d\n",ans);
for(int i=0;i<n;i++)
printf("%d ",b[a[i]]);
printf("%d\n",b[a
]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: