CF1744D 题解
洛谷传送门 & CF 传送门
思路
首先,我们可以看每个
代码
# include <bits/stdc++.h>
using namespace std;
int t, n, x, sum, a[20], tot;
int f () {
cin >> n;
memset (a, 0, sizeof a); //多组数据要清空
tot = sum = 0; //多组数据要清零
for (int i = 1; i <= n; ++ i) {
cin >> x;
while (! (x & 1)) //拆 a[i]
x >>= 1, ++ sum;
x = i;
while (! (x & 1)) 拆 i
x >>= 1, ++ tot;
++ a[tot];
tot = 0;
}
if (sum >= n)
return 0;
for (int i = 19; i; -- i) { // 倒着枚举,2 多的先操作
x = sum + a[i] * i;
if (x >= n) //满足要求
return tot + (n - sum + i - 1) / i;
tot += a[i], sum = x;
}
return -1;
}
int main () {
cin >> t;
while (t --)
cout << f () << '\n';
return 0;
}