[CF1303D] Fill The Bag - 贪心

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 - 贪心的更多相关文章
- 题解【CF1303D Fill The Bag】
\[ \texttt{Preface} \] 不开 long long 见祖宗. \[ \texttt{Description} \] 你有一个 \(n\) 码的袋子,你还有 \(m\) 个盒子,第 ...
- 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 ...
- 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 ...
- Uva 11520 - Fill the Square 贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- Codeforces1303D. Fill The Bag
1e18对应2进制有58位,可以直接暴力模拟,因为读入的数都是2次幂,__builtin_ctz这个内置gcc函数可以算出二进制下末尾有几个0,读入时统计,然后从n的最低位开始判断,注意每次升位的时候 ...
- I - Fill The Bag codeforces 1303D
题解:注意这里的数组a中的元素,全部都是2的整数幂.然后有二进制可以拼成任意数.只要一堆2的整数幂的和大于x,x也是2的整数幂,那么那一堆2的整数幂一定可以组成x. 思路:位运算,对每一位,如果该位置 ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- 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 ...
- Codeforces题解集 1.0
记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...
随机推荐
- 03(a)多元有约束优化问题(准备知识)
转成Latex上传太麻烦,直接截图上传了,需要电子版的可以关注一下,微信公众号:“实干小海豹”,回复:”优化01a“,”优化01b“,”优化02a“,”优化02b“,”优化02c“,”优化02c“.. ...
- linux 学习操作小计
屌丝最近在接触lamp开发 把工作中遇到的 问题和 一些常用的操作记下来.以便以后去翻阅 (1)linux下备份mysql数据库方法 #mysqldump -u root -p dbname > ...
- ospfv3 lsa database
https://www.networkfuntimes.com/ospfv3-the-new-lsa-types-in-ipv6-ospf/ WHY DID THEY CHANGE THE LSAs ...
- docker pull 时报错Create more free space in thin pool or use dm.min_free_space option to change behavior
docker pull 时报错: failed to register layer: devmapper: Thin Pool has 107394 free data blocks which is ...
- 安利自己写的easy-clipboard库
概述 clipboard.js 是一个非常好用的剪切板插件,但是随着前端框架的演变,用户与网页交互的方式越来越多,不仅限于点击事件了,并且在很多情况下,我们可能不需要它强制性自带的点击事件,所以我打算 ...
- AVR单片机教程——示波器
本文隶属于AVR单片机教程系列. 在用DAC做了一个稍大的项目之后,我们来拿ADC开开刀.在本讲中,我们将了解0.96寸OLED屏,移植著名的U8g2库到我们的开发板上,学习在屏幕上画直线的算法, ...
- leetcode—js—Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- Android中点击按钮获取string.xml中内容并弹窗提示
场景 AndroidStudio跑起来第一个App时新手遇到的那些坑: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797 ...
- 使用HSSFWorkbook导出、操作excel
原文地址:https://www.jianshu.com/p/dd1e4f28757b 在实际开发中我们经常需要导入数据,统计数据,并且将统计好的数据导出excel,今天分享一个导出学生信息的方法. ...
- Python3.7+Pycharm+cuda10.0+tensorflow GPU版本 安装
处理器:I5-7500 显卡 :GTX1050Ti 系统 :Win10 1. 首先搭建Python环境. 官网https://www.python.org/downloads/下载Python ...