Problem descriptionthere is a special number sequence which has n + 1 integers. For each number in sequence, we have two rules:
● AI in [0, N]
● AI = AJ (I = J)
For sequence a and sequence B, the integrating Degree t is defined as follows ("writable" denotes exclusive or ):
T = (A0 rjb0) + (A1 rjb1) + · + (an 1_bn)
(Sequence B shoshould also satisfy the rules described abve)
Now give you a number N and the sequence A. You shoshould calculate the maximum integrating Degree t and print the sequence B.
Inputthere 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,...,.
Outputfor 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
42 0 1 4 3
Sample output
201 0 2 3 4
Source2014 ACM/ICPC Asia Regional Xi 'an online
Idea: Start from the largest number to find the number that can be paired to make them unique or the maximum value. Fortunately, I didn't hit the game because Longlong wa had several rounds.
#include <stdio.h>int num[100005],d[100005];int main(){ int n,i,j,t; long long ans; while(~scanf("%d",&n)) { for(i=0;i<=n;i++) d[i]=-1; for(i=0;i<=n;i++) scanf("%d",&num[i]); ans=0; for(i=n;i>=0;i--) { if(d[i]==-1) { t=0; for(j=0;;j++) { if(!(i&(1<<j))) t+=1<<j; if(t>=i) break; } t-=(1<<j); ans+=(i^t)+(i^t); d[i]=t; d[t]=i; } } printf("%I64d\n",ans); for(i=0;i<n;i++) printf("%d ",d[num[i]]); printf("%d\n",d[num[n]]); }}
The HDU-5014-Number sequence.