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. POP and IMAP - Post Office Protocol and Internet Message Access Protocol

    POP and IMAP - Post Office Protocol and Internet Message Access Protocol 用来从 SMTP Server 上下载邮件的协议. P ...

  2. 在IDEA中如何使用lombok插件

    lombok 插件 lombok是一款可以精减java代码.提升开发人员生产效率的辅助工具,利用注解在编译期自动生成 setter/getter/toString()/constructor之类的代码 ...

  3. pytorch之 RNN regression

    关于RNN模型参数的解释,可以参看RNN参数解释 1 import torch from torch import nn import numpy as np import matplotlib.py ...

  4. stm32f407使用Keil uV5建立工程日志

    目录结构 Common           ——包括延时函数等公用函数 STM32F4_FWLIB     ——固件库 Project             ——UV5工程相关文件 Main     ...

  5. hadoop local、伪分布 模式

    local模式: 把安装包 解压到 /usr/local  下 [hadoop@master local]$ sudo tar -zxvf hadoop-2.7.3.tar.gz   创建一个软连接 ...

  6. 软件bug描述(web)

    1.bug编码与名称:测试日期+bug字段 2.测试环境:浏览器:全部/IE8,操作系统:win7 x64 3. 测试数据:用户名,密码,相关的业务账号 4.重现步骤:缺陷发现的过程 5. 缺陷说明: ...

  7. mysql出现 Unknown column 'Password' in 'field list'

    linux安装了mysql之后初始化密码获取:出现了下面的内容,密码很尴尬,无法用root登录: grep 'temporary password' /var/log/mysqld.log [Note ...

  8. 【HDU - 1260 】Tickets (简单dp)

    Tickets 搬中文 Descriptions: 现在有n个人要买电影票,如果知道每个人单独买票花费的时间,还有和前一个人一起买花费的时间,问最少花多长时间可以全部买完票. Input 给出 N(1 ...

  9. 手把手教你快速使用数据可视化BI软件创建全球经济贸易分析大屏

    灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件.   本文以全球经济贸易分析大屏为例 ...

  10. C#设计模式学习笔记:(14)命令模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7873322.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第二个模式--命 ...