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 Array数组 遍历 四种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...
- 取长文本 READ_TEXT
****取长文本 FORM GET_TEXT USING TDID TDNAME. SELECT SINGLE mandt tdobject tdname tdid tdspras INTO ...
- UniEAP Platform V5.0 Unable to compile class for JSP
流程设计器报错: http://127.0.0.1:8080/framework/workflow/webdesign/procmodify/procmodifydetail.jsp?isLoadDa ...
- window10安装Elasticsearch及可视化工具es header
1.下载es(选择windows版本) https://www.elastic.co/cn/downloads/elasticsearch 2.解压安装包,到bin目录下,运行elasticsearc ...
- C++笔记——类(0)定义、访问控制、友元、default、mutable、构造函数
整理一下一些关于类的知识点,毕竟还是很经常用的(先总结一部分,太多了). 定义格式.访问控制 C++里面定义类的关键词有两个,一个是class,另一个是struct,他们基本没有区别,除了成员变量的默 ...
- python pytorch numpy DNN 线性回归模型
1.直接奉献代码,后期有入门更新,之前一直在学的是TensorFlow, import torch from torch.autograd import Variable import torch.n ...
- 【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
平时没事喜欢看看freebuf的文章,今天在看文章的时候,无线网总是时断时续,于是自己心血来潮就动手写了这个网络爬虫,将页面保存下来方便查看 先分析网站内容,红色部分即是网站文章内容div,可以看 ...
- DataGridViewCheckBoxColumn的Value值和EditFormatedValue值不一致
今天要做一个代码修改DataGridViewCheckBoxColumn的Value值然后再遍历获取DataGridview选中项,因为遍历的时候为了能获取跟界面一致的选项,所以判断是否选中使用的是E ...
- 解决reportNG中文乱码(转:http://www.it610.com/article/3626590.htm)
1.下载reportng源码 git clone https://github.com/dwdyer/reportng.git 2.修改AbstractReporter.java ...
- ucloud建新主机
系统盘默认20G,可调到40不增加费用.需建好主机后关机才能更改. root密码按统一的设 设好主机名,选好分组