Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
2024-09-03 23:47:41
从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位
#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 ;
}
最新文章
- redis 基础
- [转]easyui tree 模仿ztree 使用扁平化加载json
- REACT 学习
- EBS R12.2应用层关闭脚本的执行过程
- 高速公路(Highway,ACM/ICPC SEERC 2005,UVa1615)
- Windows-008-VirtualBox 安装 Win7 前沿配置详解
- node.js学习(1)
- 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
- linux命令之ps命令
- -join 和 -split 用法
- matlab中的三维坐标系与旋转
- 函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义
- win8系统特别慢的基本判断方法
- Lintcode469-Same Tree-Easy
- C# 一般处理程序下载文件
- node.js初识11
- Codeforces 839C Journey - 树形动态规划 - 数学期望
- ubuntu图形化配置
- 【转帖】如何在redhat单机服务器上运行postgresql的多个实例(howto run multiple postgresql instance on one redhat server)
- UDP丢包问题