Number Sequence
Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 697 accepted submission (s): 332
Special Judge
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
If you write a binary number x = 100100, you can always find a unique number that is different from it or get 111111. For each n-bit binary number, make it different from a number every time or get 11... 11 (N 1 ). It is the optimal solution and unique.
#include"stdio.h"#include"math.h"#include"string.h"#define LL __int64#define N 100005int a[N],p[N],b[N];int fun(int x){ int t=1; while(t<=x) t*=2; return x^(t-1);}int main(){ int i,n; while(scanf("%d",&n)!=-1) { for(i=0;i<=n;i++) scanf("%d",&a[i]); memset(p,-1,sizeof(p)); for(i=n;i>=0;i--) { if(p[i]!=-1) continue; int t=fun(i); p[t]=i; p[i]=t; } LL s=0; for(i=0;i<=n;i++) { b[i]=p[a[i]]; s+=a[i]^b[i]; } printf("%I64d\n",s); for(i=0;i<n;i++) printf("%d ",b[i]); printf("%d\n",b[n]); } return 0;}
HDU 5014 number sequence (Greedy)