Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence (changed after round)贪心

解析一道Codeforces难题,任务是将给定数值n拆分为k个2的幂次方之和,要求最大的幂次数尽可能小,且序列字典序最大。
B. Jamie and Binary Sequence (changed after round)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:

Find k integers such that the sum of two to the power of each number equals to the number n and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.

To be more clear, consider all integer sequence with length k (a1, a2, ..., ak) with . Give a value  to each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.

For definitions of powers and lexicographical order see notes.

Input

The first line consists of two integers n and k (1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) — the required sum and the length of the sequence.

Output

Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes" (without quotes) in the first line, and k numbers separated by space in the second line — the required sequence.

It is guaranteed that the integers in the answer sequence fit the range [ - 1018, 1018].

Examples
input
23 5
output
Yes
3 3 2 1 0 
input
13 2
output
No
input
1 2
output
Yes
-1 -1 
Note

Sample 1:

23 + 23 + 22 + 21 + 20 = 8 + 8 + 4 + 2 + 1 = 23

Answers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are not lexicographically largest.

Answers like (4, 1, 1, 1, 0) do not have the minimum y value.

Sample 2:

It can be shown there does not exist a sequence with length 2.

Sample 3:

Powers of 2:

If x > 0, then 2x = 2·2·2·...·2 (x times).

If x = 0, then 2x = 1.

If x < 0, then .

Lexicographical order:

Given two different sequences of the same length, (a1, a2, ... , ak) and (b1, b2, ... , bk), the first one is smaller than the second one for the lexicographical order, if and only if ai < bi, for the first i where ai and bi differ.

有点棘手的一道题,光题面就读了好久QAQ

题意:给一个数n,将其分为k个“2的若干次幂”数之和,要求最大的幂次数尽可能小,且要求序列的字典序尽可能大

思路:首先将n的二进制形式找出,如果二进制中的1的个数大于k,则无法形成题目要求的序列;

  小于k时,就要将1的个数扩展到k个。由于一个高的二进制位可以转化为两个低二进制位的和,故首先将高次幂尽可能全部转化为低次幂,全部转化的原因是因为需要保持序列的字典序最大,故要么全部转化,要么全不转。此时,序列的个数可能还不足k个,这时就需要将最低位的次幂转化为更低的次幂,用以扩充序列的个数直到达到k个,这时最低位的次幂数就不用全部转化了;

 等于k时,1的个数恰好等于k,无法对序列进行修改,故直接输出即可

 代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6;
int p[200];    //p的后100位代表2的正幂次,前100位代表2的负幂次

int main()
{
    ll n,k,ans=1;
    while (~scanf("%I64d %I64d",&n,&k)){
        memset(p,0,sizeof(p));
        int plen=100,pcnt=0;
        //将n的二进制形式表示出
        while (n){
            p[plen++]=n%2;
            if (n%2)
                pcnt++;     //记录二进制中1的个数
            n>>=1;
        }
        //k小于1的个数时不成立
        if (pcnt>k){
            printf("no\n");
            continue;
        }
        //等于时直接输出
        else if (pcnt==k){
            printf("yes\n");
            for (int i=plen;i>=100;i--){
                if (p[i])
                    printf("%d ",i-100);
            }
            printf("\n");
        }
        //大于时,需要扩充1的个数
        else{
            ll t=k-pcnt;        //剩余需要填充的数量
            //一个高的2次幂可以转化为两个低二次幂之和
            while (t){
                if (t<p[plen])
                    break;
                t-=p[plen];
                p[plen-1]+=p[plen]*2;
                p[plen]=0;
                plen--;
            }
            //用优先队列保存下来
            priority_queue<ll,vector<ll>,greater<ll> >q;
            for(int i=64;i>=-100;i--){
                if (p[i+100]){
                    for (int j=0;j<p[i+100];j++)
                        q.push((ll)i);
                }
            }
            //将一个最低位的二次幂转化为两个更低的二次幂,用以填充序列的个数
            while (t){
                ll now=q.top();
                t--;
                q.pop();
                q.push(now-1);
                q.push(now-1);
            }
            printf("yes\n");
            vector<ll> v;
            while(!q.empty()){
                v.push_back(q.top());
                q.pop();
            }
            for (int i=v.size()-1;i>=0;i--)
                printf("%d ",v[i]);
            printf("\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值