CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)
题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大。
析:直接想可能不好想,可以考虑,如果把数字 n 拆成 2 的多次幂,可以用贪心来解决,然后如果长度已经超过了 m ,那么就是无解,否则就是有解,再考虑把这个序列变成 m 长度,因为要让最大的最小,所以,可以把最大的拆成两个,然后再看里面最大的是几个,如果序列当前长度加上序列中最大的数的数目,仍然不超过m,那么,就可以把最大的数再拆成两个,这样就保证了最大的傎尽量小。如果序列当前长度加上序列中最大的数的数目,超过了 m,那么最大数肯定就是当前最大的了,要保证字典序最大,所以要拆最小的数,一直拆最小的就可以了,因为,最小的数一直可以拆,直到序列长度为m。最后输出就可以了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 3e5 + 10;
const LL mod = 1e9 + 7LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int main(){
LL n;
scanf("%lld %d", &n, &m);
multiset<int> sets;
for(int i = 63; i >= 0; --i)
if(n >= (1ULL<<i)) sets.insert(i), n -= 1ULL<<i;
if(sets.sz > m){ puts("No\n"); return 0; }
while(sets.sz < m){
int t = *sets.rbegin();
if(sets.sz == 1){
sets.erase(t);
sets.insert(t-1);
sets.insert(t-1);
}
else{
int num = sets.count(t);
if(num <= m - sets.sz){ // split the biggest one
sets.erase(*sets.rbegin());
for(int i = 0; i < num * 2; ++i) sets.insert(t-1);
}
else{ // not split
t = *sets.begin();
sets.erase(sets.begin());
--t;
while(sets.sz + 2 < m){
sets.insert(t);
--t;
}
sets.insert(t);
sets.insert(t);
}
}
}
puts("Yes");
int i = 1;
for(multiset<int> :: reverse_iterator it = sets.rbegin(); it != sets.rend(); ++it)
printf("%d%c", *it, " \n"[i == m]);
return 0;
}
CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)的更多相关文章
- 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-916B-Jamie and Binary Sequence(changed after round)(构造)
链接: https://vjudge.net/problem/CodeForces-916B 题意: Jamie is preparing a Codeforces round. He has got ...
- 【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 ...
随机推荐
- LINUX SSH修改默认22/添加端口
通常ssh远程登录的默认端口是22,但是因为端口22属于高危端口,因此很多时候作为服务器会被关掉,不过这个端口一般是可以更改或者添加的,这样除了22端口,也可以通过别的端口进行访问. 1.首先修改配置 ...
- Android系统显示原理
Android的显示过程可以概括为:Android应用程序把经过测量.布局.绘制后的surface缓存数据,通过SurfaceFlinger把数据渲染到屏幕上,通过Android的刷新机制来刷新数据. ...
- android的图片的初步学习理解
Android支持JPEG和PNG格式.GIF和BMP格式图片的支持. 图片最终要显示在屏幕上,都会对应一个屏幕上的点,即对应一个颜色值.不同格式的图片,只是不同压缩编码和解压算法. 也就是说,我们看 ...
- solr简介、学习详细过程!(超详细~)
solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...
- thymeleaf 格式化时间
运用Thymeleaf模板后,前台的时间显示发生变化,和数据库不一致 HTML页面中格式如下: <td th:text="${fleeceRecord.cashmereDate}&qu ...
- 安装scrapy 出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools 错误
安装scrapy 出现以下 错误: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C ...
- 24.Mysql高级安装和升级
24.Mysql高级安装和升级24.1 Linux/Unix平台下的安装 24.1.1 安装包比较Linux下的Mysql安装包分为RPM包.二进制包.源码包3种.RPM包优点是安装简单,适合初学者: ...
- Python 官方文件
7.2. 文件读写 函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode). >>> f = open('workfile', 'w') ...
- C# System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
好好的程序,突然出现了错误,原因是:System.Data.OracleClient requires Oracle client software version 8.1.7 or greater, ...
- Java.Class
Class类 1. Class继承自Object. 2. .class 和 instance.getClass()的区别 Ref[1] Reference 1. .class http://stack ...