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

 #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. Linux指令(Terminal命令):

    刚开始学习Linux,整理了一下常用指令和快捷键,不足的日后补充. 指令:# cd:进入目录 pwd:查看当前所在路径 ls:查看当前所在目录下的所有子目录或者子文件 ls-l     |     l ...

  2. pandas学习笔记之删除指定列

    删除指定 def df["列名"] del df4["韩国地震影响"] 直接删除,df4中不在含有"韩国地震影响"这一列了 drop 不改变 ...

  3. arcgis10.2下载安装教程

    ArcGIS Desktop 10.2 完全安装教程(含win7 32/64位+下载地址+亲测可用) 时间: 2014年08月20日   阅读: 622,262   分类: GIS探秘   标签: A ...

  4. react-路由和Ant design

    路由的使用 react-router import React from 'react' // 如果要使用 路由模块,第一步,运行 yarn add react-router-dom // 第二步,导 ...

  5. python3相关

    with用法 with A as B: 语句 调用( A返回的类) 的__enter__方法 将该方法返回值赋予B, 执行完with语句块后, 调用类的__exit__方法

  6. R语言函数化编程笔记1

    R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...

  7. 《深入理解java虚拟机》读书笔记九——第十章

    第十章 早期(编译期)优化 1.Javac的源码与调试 编译期的分类: 前端编译期:把*.java文件转换为*.class文件的过程.例如sun的javac.eclipseJDT中的增量编译器. JI ...

  8. EL表达式无法获取boolean类型变量值

    今天调试个程序, 有个名为isAdmin的boolean类型的变量在jsp页面获取到的值为空, 这根本就是没获取到或者变量不存在的状况啊,但是在Action中明明是赋值成false了. 上网查了一下有 ...

  9. java的开发步骤----

    首先老师给了一个 视频给我:叫我理解java的简单开发步骤: 上两张图: 然后我就理解成了这样::: 简述java开发步骤:第一步: 编写源代码,把源代码写好 :第二部:源代码通过编译器编译成字节码c ...

  10. K3老单序时簿开发示例

    K3需要对老单进行二次开发,老单的二次开发比较麻烦,这里整理一下老单序时簿上添加按钮的二次开发示例. --以下SQL脚本--获取 MENU IDselect FID,FmenuID,FName fro ...