从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int sum[],num[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
memset(sum,,sizeof(sum));
memset(num,,sizeof(num));
long long n;
int m;
cin>>n>>m;
long long x=n;
int cnt2=;
while(n){
if(n&)
++sum[cnt2];//将n转化为二进制
n/=;
++cnt2;
}
long long res=;
for(int i=;i<=m;++i){
cin>>a[i];
res+=a[i];
int cnt=-;
while(a[i]){
a[i]/=;
++cnt;
}
++num[cnt];//将a[i]转化为二进制
}
if(res<x){
cout<<"-1\n";
continue;
}
long long ans=;
for(int i=;i<;++i){//从低位向高位枚举
if(sum[i]){
if(num[i])
--num[i];//如果有对应a[i]直接取
else{
for(int j=i+;j<;++j){//没有的话就找到最近的一个a[i]
if(num[j]){
--num[j];//把它一步步拆掉,直到二进制下i位有一个可取
for(int k=i;k<j;++k){
++num[k];
}
ans+=j-i;//一共拆了j-i次
break;
}
}
}
}
num[i+]+=num[i]/;//多余的a[i]向上合并成为更大的a[i+1]
}
cout<<ans<<"\n";
}
return ;
}

Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)的更多相关文章

  1. Educational Codeforces Round 82 (Rated for Div. 2)

    题外话 开始没看懂D题意跳了,发现F题难写又跳回来了.. 语文好差,码力好差 A 判第一个\(1\)跟最后一个\(1\)中\(0\)的个数即可 B 乘乘除除就完事了 C 用并查集判一下联通,每个联通块 ...

  2. 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 ...

  3. Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],t[]; int n,m; ][]; ...

  4. [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)

    A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. hextorgb

    function hexToRgb(hex) { // By Tim Down - http://stackoverflow.com/a/5624139/3493650 // Expand short ...

  2. gulp常用插件之gulp-notify使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-notify这是一款gulp通知插件. 更多使用文档请点击访问gulp-notify工具官网. 安装 一键安装不多解释 npm inst ...

  3. sqli-labs less-15 --> less-16

    Less-15(布尔/时间盲注) 1.判断是否存在注入点 无论是输入admin/admin’/admin” 都显示回显正常,于是执行uname=admin' and sleep(5)#&pas ...

  4. SQL语句中设置字段值取反操作

    1.对布尔值取反,使用 ~. 如 update set status=~status where id=2; status的值为true || false. 2.对0.1 数值取反,使用abs() 取 ...

  5. StarUML 2下载、安装、破解全过程

      StarUML官方下载地址:  http://staruml.io/download 破解: ​  1.使用Editplus或者Notepad++等特殊的文本编辑器打开 安装位置下/www/lic ...

  6. 看完它,你就全懂了十大Wifi芯片原厂!

    看完它,你就全懂了十大Wifi芯片原厂!   来源:全球物联网观察 概要:不知不觉中,WiFi几乎已攻占了整个世界.现在只要你上网,可能就离不开WiFi了. 2014年是物联网WiFi市场关键的转折期 ...

  7. Docker最全教程——从理论到实战(二十三)

    如何节约云端成本? 上云在大部分情况下就是为了降低成本,在这方面,主流的容器服务基本上都能够有效地降低成本——不仅能够高效自动化的管理和控制容器,而且不需支付Kubernetes 主节点的费用.不过, ...

  8. Flutter简易顶部导航

    因为在AppBar的bottom参数中返回TabBar在平板模式的对齐模式是居中的且不可调整,所有将TabBar在title中返回 import 'package:flutter/material.d ...

  9. jQuery---自定义动画 animate();

    自定义动画 animate(); 第一个参数:{对象},里面可以传需要动画的样式 第二个参数:speed 动画的执行时间 第三个参数:easing 动画的执行效果 第四个参数:callback 回调函 ...

  10. Python入门10 —— for循环

    1.字符串依次取值 students = ['egon', 'lxx', 'alex'] i = 0 while i < 3: print(students[i]) i += 1 2.针对循环取 ...