Hdu5014: Number Sequence Symmetry

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.