CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)
链接:
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)(构造)的更多相关文章
- CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)
题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大. 析: ...
- Codeforces 916B - Jamie and Binary Sequence (changed after round)
思路: 先取出二进制的每一位,判断总个数是不是小于等于k,如果大于k则不能构成. 通过观察可以发现,每一位的一个可以转换成下一位的两个,因为要使最大位尽可能小,所以如果最大位的所有的个数都可以转换成下 ...
- Jamie and Binary Sequence (changed after round) - CodeForces 916B
http://codeforces.com/problemset/problem/916/B 好尬啊... #include<cstdio> #include<algorithm&g ...
- Jamie and Binary Sequence (changed after round) CodeForces - 916B (贪心)
链接 大意: 求将n划分为k个2的幂的和, 且最大幂最小,字典序尽量大 比较简单的贪心练习题, 但放在div2的B题感觉偏难了..... 先只考虑最大幂最小, 首先注意到直接按n的二进制划分即可得到最 ...
- Codeforces 916B Jamie and Binary Sequence ( 模拟 && 思维 )
题意 : 给出一个数 n ,要求你用 k 个二的幂来组成这个数,要求输出这 k 个二的幂的指数,如果有多解情况则优先输出最大指数最小的那一个且要求按字典序输出,不存在则输出 No 分析 : 先来说一 ...
- 【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把n分解成二进制的形式. n=2^a0+2^a1+...+2^a[q-1] 则固定就是长度为q的序列. 要想扩展为长为k的序列. 可 ...
- ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)
二叉树 在计算机科学中,二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用作二叉查找树和二叉堆或是 ...
- 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 ...
- codeforces 487C C. Prefix Product Sequence(构造+数论)
题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
随机推荐
- Java学习之==>IO文件操作体系
一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...
- mxml 嵌入as代码出错,缺少 CDATA
如果<mx:Script> 中有大于小于符号,代码必须包含在<![CDATA[ 之中,否则会报错
- 【C++学习笔记】static 关键字
(阅读<C++ primer plus>可知 C++的static关键字跟Java还是很类似的) 为什么需要static关键字:在文件A中定义的非局部变量language,在文件B中可以通 ...
- Linux进程: task_struct结构体成员
一:简介 为了管理进程,内核必须对每个进程所做的事情进行清除的描叙. 比如:内核必须知道进程优先级,他是正在CPU上运行还是因为某些事件被阻塞了,给它分配了什么样的地址空间,允许它访问哪个文件等等.这 ...
- 什么是token?你是怎么理解token?
1.Token的引入: Token是在客户端频繁向服务端请求数据,服务端频繁的去数据库查询用户名和密码并进行对比,判断用户名和密码正确与否,并作出相应提示,在这样的背景下,Token便应运而生. 2. ...
- Windows Server 2008 R2忘记管理员密码后的解决方法
在日常的工作中,对于一个网络管理员来讲最悲哀的事情莫过于在没有备用管理员账户和密码恢复盘的情况下遗忘了本地管理员账户密码.在早期的系统中,遇到这种事情可以使用目前国内的很多Windows PE光盘来解 ...
- [官网]关于EPEL
EPEL/zh-cn https://fedoraproject.org/wiki/EPEL/zh-cn Contents [hide] 1企业版 Linux 附加软件包(EPEL) 1.1什么是企 ...
- 思考--PostgreSQL在与mysql的比较中稍微弱势项
PostgreSQL在与mysql的比较中稍微弱势项: 1.都是堆表,没有所谓的聚集索引表,其实问题不大,聚集索引表也只是在使用聚集索引那些列有加速,而且pg也有聚集索引,只不过要定期重建. 2.mv ...
- oracle 数据库备份与恢复
oracle 数据库备份与恢复 包含四个部分: 1.数据泵备份与恢复 2.rman备份与恢复 3.CSV增量备份恢复 4.截库操作 1.数据泵备份与恢复 expdp/ / impdp 时的 CONTE ...
- wode.
http://www.cnblogs.com/wilber2013/p/4638967.html