Educational Codeforces Round 82 (Rated for Div. 2)D. Fill The Bag

本文解析了一道算法竞赛题目,目标是最小化分割次数以使用一系列2的幂次方数值填充指定大小的背包。通过分析数字的二进制表示和计数不同位上数值的数量,提供了一个高效的解决方案。

D. Fill The Bag
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have a bag of size n. Also you have m boxes. The size of i-th box is ai, where each ai is an integer non-negative power of two.

You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.

For example, if n=10 and a=[1,1,32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.

Calculate the minimum number of divisions required to fill the bag of size n.

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains two integers n and m (1≤n≤1018,1≤m≤105) — the size of bag and the number of boxes, respectively.

The second line of each test case contains m integers a1,a2,…,am (1≤ai≤109) — the sizes of boxes. It is guaranteed that each ai is a power of two.

It is also guaranteed that sum of all m over all test cases does not exceed 105.

Output
For each test case print one integer — the minimum number of divisions required to fill the bag of size n (or −1, if it is impossible).

Example
inputCopy
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
outputCopy
2
-1
0

题意:就是给了一个数字n,然后给了m个数,这m个数都是2的幂次方
现在每次操作你可以把m个数中的一个一分为2,比如32 操作一次得到两个16,在操作一次就是4个8,
这样是操作了两次,现在问你 最少要操作多少次,可以在操作后把这些数字恰好凑为n

思路:显然 如果m个数总和<n 自然就是输出-1
如果可以的话我们考虑二进制,先把这m个数,用cnt[i]存下来 cnt[i]表示二进制从低到高 第i位有多少个数
然后 我们对于n进二进制,
如果对于n当前该二进制位i是1.,我们去看cnt[i]是不是有没有,有的话,自然就不用把其他数拆分为两个原来的一半,然后如果该为有剩余,每两个可以凑成一个cnt[i+1]
如果cnt[i]为0,我们就ans++,然后cnt[i+1]–,因为我们需要实现一个拆分
所以最后答案就是ans 也就是拆分的次数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt[70];
int main()
{
    int t;cin>>t;
    while(t--)
    {
      memset(cnt,0,sizeof cnt);
      ll n,m;
      cin>>n>>m;
      ll sum=0;
      for(int i=0;i<m;i++){

          int num=0,x;
          cin>>x;
          sum+=x;
          while(x) num++,x>>=1;
          cnt[num-1]++;

      }
      if(sum<n) {puts("-1");continue;}
      else
      {
          int ans=0;
          for(int i=0;i<=63;i++){

             int x=(n>>i)&1;
             cnt[i]-=x;
             if(cnt[i]>=2) cnt[i+1]+=cnt[i]/2;
             if(cnt[i]<0) ans++,cnt[i+1]--;
          }
          cout<<ans<<endl;
      }
    }
    return 0;
}

### 关于Codeforces Educational Round 172 Problem D 的解决方案 对于Codeforces Educational Round 172中的D题,虽然未直接提供该题目具体描述以及官方解答[^2],可以基于过往相似难度和类型的题目给出一般性的解决思路。 #### 题目分析 通常情况下,D级别的题目会涉及到较为复杂的算法设计或是数据结构的应用。这类问题往往需要参赛者具备良好的编程基础、逻辑思维能力以及对特定算法的理解掌握程度。针对不同性质的问题(如图论、动态规划、字符串处理等),采取相应的策略来构建模型并求解是最常见的方法之一。 #### 解决方案框架 假设此题属于某种典型问题类别,则可以根据其特点制定如下通用框架: - **输入解析**:仔细阅读题目说明,明确给定条件与目标函数之间的关系。 - **核心概念理解**:深入剖析题目背后所隐藏的关键知识点或技巧点,这可能涉及但不限于贪心算法、二分查找、树形DP等方面的知识。 - **边界情况考虑**:考虑到极端测试用例的存在,在编写程序时要特别注意各种特殊情况下的行为表现,确保代码鲁棒性强。 - **优化空间复杂度/时间效率**:当面对大数据集时,应尽可能寻找更高效的实现方式减少不必要的计算开销;比如利用哈希表加速查询速度,通过位运算代替常规算术操作提高性能等等。 ```cpp // 假设这是一个简化版的伪代码示例 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; // 输入参数 vector<int> data(n); for(auto& d : data){ cin>>d; } // 主体逻辑部分省略... cout << "Result"; return 0; } ``` 由于缺乏具体的题目细节,上述内容仅作为参考模板展示如何着手准备类似的竞赛挑战。为了获得更加精准的帮助建议查阅官方题解文档或者参与社区论坛交流获取更多信息资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值