hdu 5014 number sequence 贪心

题目链接:点击打开链接

题意:有数列a和数列b由1~n组成,给定a数列,问如何排列b,才能使sum(ai⊕bi)最大。

思路:把1~n的二进制表示输出一遍会发现规律;

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int get_lentgh(const int& n)
{
    for(int i=0;i<n;i++)
    {
        if((n>>i)==1)
            return i;
    }
}
void read(int& a)
{
    char c;
    while (c=getchar(),c<'0'||c>'9');
    a=c-'0';
    while (c=getchar(),c>='0'&&c<='9')
        a=a*10+c-'0';
}
int main()
{
    int n,ys[100010];
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    while(scanf("%d",&n)!=EOF)
    {
        ys[0]=0;
        int bj=n;
        while(bj>0){
            int l=get_lentgh(bj);
            int temp=(1<<l);
            int cnt=bj-(temp-1);
            for(int i=0;i<cnt;i++)
            {
                ys[i+temp]=temp-i-1;
                ys[temp-i-1]=i+temp;
            }
            bj=2*temp-bj-2;
        }
        printf("%I64d\n",(ll)n*(n+1));
        int tp;
        for(int ie=0;ie<n;ie++)
        {
            read(tp);
            printf("%d ",ys[tp]);
        }
        read(tp);
        printf("%d\n",ys[tp]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值