Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5014
The array a [] = {0, 1, 2 ...... n} evaluate an array B [] element is also 0 ..... n, but the order is different from that of [].
Maximum Sum (AI ^ Bi)
Note 2 ^ K = 100000 (K 0) 2 ^ k-1 = 11111 (K 1)
So (2 ^ K) ^ (2 ^ k-1) = 111111 (k + 1 1) equals 2 ^ (k + 1) -1 similarly has (2 ^ k + 1) ^ (2 ^ K-2) = 2 ^ (k + 1)-1;
In this case, the "1" in the element is fully utilized, and the result is the maximum value.
Therefore, you only need to consider the symmetric allocation of each integer less than or equal to n to the power of 2.
The Code is as follows:
#include <iostream>#include <stdio.h>#include <memory.h>#include<string.h>#include<algorithm>#include<string>#include<ctype.h>using namespace std;#define MAXN 10000int p[17]={1,2 ,4 ,8 ,16 ,32, 64, 128, 256, 512 ,1024, 2048, 4096 ,8192 ,16384 ,32768,65536};bool vi[100010];int a[100010];int b[100010];int ans[100010];int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(vi,0,sizeof(vi)); long long res=0; for(int i=0;i<=n;i++) scanf("%d",a+i); int k; for(k=16;k>=0&&p[k]>n;k--); while(k>=0) { int i=p[k]-1; int j=p[k]; for(;i>=0&&j<=n;i--,j++) { if(vi[i]||vi[j]) break; res+=2*(j^i); ans[i]=j; ans[j]=i; vi[i]=1; vi[j]=1; } k--; } if(vi[0]==0) ans[0]=0; printf("%I64d\n",res); for(int i=0;i<=n;i++) { printf("%d",ans[a[i]]); if(i==n) printf("\n"); else printf(" "); } } return 0;}
Hdu5014: Number Sequence Symmetry