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

时间:2025-02-11 18:37:14

题意:给定两个数字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;
}