链接:

https://vjudge.net/problem/CodeForces-916B

题意:

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.

思路:

先构造出最少数量的序列,如果此时长度大于m,就是没有可能.

否则优先判断前面较大的能不能全部用掉,否则从小的开始去减.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; LL n, m; int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
multiset<int> st;
for (int i = 63;i >= 0;i--)
{
if ((1ULL<<i) <= n)
{
st.insert(i);
n -= (1LL<<i);
}
}
if (st.size() > m)
puts("No");
else
{
while (st.size() < m)
{
if (st.size() == 1)
{
int t = *st.rbegin();
st.erase(*st.rbegin());
st.insert(t-1);
st.insert(t-1);
}
else
{
int cnt = st.count(*st.rbegin());
if (m-st.size() >= cnt)
{
int t = *st.rbegin();
st.erase(t);
for (int i = 1;i <= cnt*2;i++)
st.insert(t-1);
}
else
{
int t = *st.begin();
t--;
st.erase(st.begin());
while (st.size()+2 < m)
{
st.insert(t);
t--;
}
st.insert(t);
st.insert(t);
}
}
}
cout << "Yes" << endl;
for (multiset<int>::reverse_iterator it = st.rbegin();it != st.rend();++it)
cout << *it << ' ';
cout << endl;
} return 0;
}

CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)的更多相关文章

  1. CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)

    题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大. 析: ...

  2. Codeforces 916B - Jamie and Binary Sequence (changed after round)

    思路: 先取出二进制的每一位,判断总个数是不是小于等于k,如果大于k则不能构成. 通过观察可以发现,每一位的一个可以转换成下一位的两个,因为要使最大位尽可能小,所以如果最大位的所有的个数都可以转换成下 ...

  3. Jamie and Binary Sequence (changed after round) - CodeForces 916B

    http://codeforces.com/problemset/problem/916/B 好尬啊... #include<cstdio> #include<algorithm&g ...

  4. Jamie and Binary Sequence (changed after round) CodeForces - 916B (贪心)

    链接 大意: 求将n划分为k个2的幂的和, 且最大幂最小,字典序尽量大 比较简单的贪心练习题, 但放在div2的B题感觉偏难了..... 先只考虑最大幂最小, 首先注意到直接按n的二进制划分即可得到最 ...

  5. Codeforces 916B Jamie and Binary Sequence ( 模拟 && 思维 )

    题意 : 给出一个数 n ,要求你用 k 个二的幂来组成这个数,要求输出这 k 个二的幂的指数,如果有多解情况则优先输出最大指数最小的那一个且要求按字典序输出,不存在则输出 No 分析 :  先来说一 ...

  6. 【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把n分解成二进制的形式. n=2^a0+2^a1+...+2^a[q-1] 则固定就是长度为q的序列. 要想扩展为长为k的序列. 可 ...

  7. ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)

    二叉树 在计算机科学中,二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用作二叉查找树和二叉堆或是 ...

  8. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  9. codeforces 487C C. Prefix Product Sequence(构造+数论)

    题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

随机推荐

  1. 新增存储用Parted分区并建LVM卷

    新增存储用Parted分区并建LVM卷 一,Parted分区 1,parted分区  www.ahlinux.com # parted /dev/sda GNU Parted 2.1 使用 /dev/ ...

  2. MySQL 数据库架构

    1 架构 master-slave性能,qps,tps,cpu,disk,memory,network,raid卡,fashion卡, 并发量:同一时刻需要db处理 连接量: 大表:1000w行或者表 ...

  3. GoLand远程Linux开发环境搭建

    Goland 远程调试本文介绍如何从本机的goland连接远端server上的go代码进行调试 goland下载安装 建议购买正版,科学使用自行搜索. 需要安装插件,确保可以访问官网,不然配置下pro ...

  4. java驼峰法和下划线法字符串的相互转换

    java驼峰法和下划线法字符串的相互转换 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  5. aliyun搭博客从零到一

    一.基础环境 lnmp      1台负载均衡SLB     2台ECS    1台 RDS  二.lnmp搭建 1.#配置nginx的yum仓库 2.#yum install  -y  nginx ...

  6. switch-case分支结构总结

    1,格式 switch(表达式){ case 常量1:执行语句1: case 常量1:执行语句1: ... ... case 常量n:执行语句n: default:执行语句:} 2,说明: 根据swi ...

  7. hue数据导出

    1/ 执行要下载的sql语句 2/  执行完成后,点击下面导出 3.选择所有 4.选择每个人对应的文件夹,没有自己名称的可以创建一个 5.选择导出 6.右面是导出执行时的界面 7.选择已经执行的文件 ...

  8. http请求跨域问题分析

    http请求跨域问题分析 个人认为可能涉及很多http的知识,本人才疏学浅不敢妄自揣测,只是做个笔记为了以后对比理解,从原生fetch说起吧,前提假设有个后端服务,我用express来模拟,如下: v ...

  9. 【洛谷p1051】谁拿了最多奖学金

    谁拿了最多奖学金[题目链接] 这道题早就想做它啦. 咱也不知道为啥,咱就是看这道题特别顺眼呢qwq: MY SOLUTION: 其实这道题很简单,开一个结构体记录各项信息,然后根据条件计算出这个人获得 ...

  10. git diff 命令用法

    理解git diff的前提,首先要理解git中工作区,暂存区,本地版本库的概念,如果头脑中有这些概念,接着往下读. git diff test.c 用来查看工作区和暂存区中test.c文件的区别. g ...