Solution

考虑从低位往高位贪心,设当前在处理第 \(i\) 位,更低位剩余的部分一共可以拼出 \(cnt\) 个 \(2^i\)

如果 \(n\) 的这一位是 \(1\) ,那么这一位就需要处理

  • 如果 \(cnt>0\),那么直接从 \(cnt\) 里拿一个,答案不变
  • 否则,暴力向更高位找到最小的那一个,比如它在 \(j\) 位,那么将 \(a_j\) 减 \(1\),同时将 \(a_{j-1},...,a_{i}\) 都加上 \(1\),并且答案增加 \(j-i\)

做完每一位后,维护一下 \(cnt\) 即可

(过晚了一分钟)

#include <bits/stdc++.h>
using namespace std; #define int long long
int T,n,m,t,a[66],cnt; signed main() {
ios::sync_with_stdio(false);
cin>>T;
while(T--) {
cin>>n>>m;
memset(a,0,sizeof a);
int sum=0;
for(int i=1;i<=m;i++) {
cin>>t;
for(int j=0;j<63;j++) {
if(t==(1ll<<j)) a[j]++;
}
sum+=t;
} cnt=0;
int ans=0,fg=0;
if(sum<n) fg=1;
for(int i=0;i<63;i++) {
if((n & (1ll<<i))) {
if(cnt>0) --cnt;
else if(a[i]>0) {
--a[i];
}
else {
int j=i+1;
while(a[j]==0 && j<63) ++j;
if(j==63) {
fg=1;
break;
}
else {
ans+=j-i;
a[j]--;
for(int k=i;k<j;k++) a[k]++;
}
}
}
cnt+=a[i];
cnt/=2;
} if(fg) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
}

[CF1303D] Fill The Bag - 贪心的更多相关文章

  1. 题解【CF1303D Fill The Bag】

    \[ \texttt{Preface} \] 不开 long long 见祖宗. \[ \texttt{Description} \] 你有一个 \(n\) 码的袋子,你还有 \(m\) 个盒子,第 ...

  2. 1033 To Fill or Not to Fill (25分)(贪心)

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  3. PAT 1033 To Fill or Not to Fill (25分) 贪心思想

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  4. Uva 11520 - Fill the Square 贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. Codeforces1303D. Fill The Bag

    1e18对应2进制有58位,可以直接暴力模拟,因为读入的数都是2次幂,__builtin_ctz这个内置gcc函数可以算出二进制下末尾有几个0,读入时统计,然后从n的最低位开始判断,注意每次升位的时候 ...

  6. I - Fill The Bag codeforces 1303D

    题解:注意这里的数组a中的元素,全部都是2的整数幂.然后有二进制可以拼成任意数.只要一堆2的整数幂的和大于x,x也是2的整数幂,那么那一堆2的整数幂一定可以组成x. 思路:位运算,对每一位,如果该位置 ...

  7. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  8. Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)

    A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...

  9. Codeforces题解集 1.0

    记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...

随机推荐

  1. create-react-app 打包后静态文件过大 webpack优化

    在最近的项目里,页面和静态文件并不是很多的情况下,打包后发现产出的静态资源却很大. 1.关掉sourcemap 在config/webpack.config.js文件里,大概30几行的位置添加这样一句 ...

  2. javascript 完全正确的数据库indexedDB

    //indexedDB var dbName = 'whx', version = '1', dbTableName = 'bbg', request, db, conCls, updateKey, ...

  3. 《自拍教程13》Windows的常用命令

    这些是Windows系统自带的常用DOS命令集合, 先大概了解下,当然如果能熟练掌握那最好了. 后续思维篇,思维篇还会结合不通的测试场景, 届时将列出这些命令更详细的使用描述. table.dataf ...

  4. Spring Bean几种注入方式——setter(常用),构造器,注入内部Bean,注入集合,接口...

    依赖注入分为三种方式: 1.1构造器注入 构造器通过构造方法实现,构造方法有无参数都可以.在大部分情况下我们都是通过类的构造器来创建对象,Spring也可以采用反射机制通过构造器完成注入,这就是构造器 ...

  5. WLAN802.11

    IEEE 802.11,1997年,原始标准(2Mbit/s,播在2.4GHz).DSSS, FHSS.该协议的一大缺点是它提供了太多的选择使得互操作性很难实现.所以它不严密,更像是一个beta标准, ...

  6. 一个sio.c的实现及遇到的多任务执行问题

    https://sourceforge.net/p/cadcdev/lwip/ci/491e00038f26dc5d775f120aa49519a066819ebf/tree/kos/sio.c /* ...

  7. light oj1170 - Counting Perfect BST卡特兰数

    1170 - Counting Perfect BST BST is the acronym for Binary Search Tree. A BST is a tree data structur ...

  8. 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。

    class A(object): def __init__(self): print("init class A") class B(object): def __init__(s ...

  9. JMeter函数助手中Random函数详解

    1.__Random函数的生成 The minimum value allowed for a range of values:一个范围内允许的最小值 The maximum value allowe ...

  10. Python requests 调Jenkins登录接口,返回404,但请求地址、请求头、消息主题和抓包的内容都一样

    #coding=utf-8import requests url = "http://localhost:8080/jenkins/j_acegi_security_check"h ...