Recently Monocarp has created his own mini-laboratory!
The laboratory contains nn bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 77 merge, one bacterium with size 1414 is the result.
It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store.
You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.
Input
The first line contains one integer nn (1 \le n \le 2 \cdot 10^5)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains.
The second line contains nn integers a_{1}, a_{2}, \dots, a_{n}a1,a2,…,an (1 \le a_{i} \le 10^9)(1≤ai≤109), where a_iai is the size of the ii-th bacterium in the laboratory.
Output
If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1.
Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.
Sample 1
Inputcopy | Outputcopy |
---|---|
2 1 4 | 2 |
Sample 2
Inputcopy | Outputcopy |
---|---|
3 3 6 9 | -1 |
Sample 3
Inputcopy | Outputcopy |
---|---|
7 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 | 1 |
Note
In the first example Monocarp should buy one bacterium having size 11 and one bacterium having size 22. Then Monocarp will have 44 bacteria having sizes [1, 4, 1, 2][1,4,1,2]. Then two bacteria having sizes 11 can be merged into one having size 22. Then Monocarp will have 33 bacteria having sizes [2, 4, 2][2,4,2]. Then two bacteria having sizes 22 can be merged into one having size 44. Then Monocarp will have 22 bacteria having sizes [4, 4][4,4], which can be merged into one having size 88.
In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria.
In the third example Monocarp needs to buy one bacterium having size 10000000001000000000.
题意:大体意思是说让你可以随意添加相同大小的元素,两个相同大小的元素可以合并成一个大小为原来两倍的元素,问最少需要添加多少个元素,如果最后合并不成一个输出-1.
方法:1.map迭代法(最先想到的)同时也Tle了
错因分析:用map时注意大小,因为不断迭代,所以可能爆int,本题爆了int
时间复杂度:遍历map时用迭代器遍历,当map更新时,迭代器会继续进行更新。
分析问题方面:只考虑了最小值和其它值的关系,如果中间还有一个和其它元素的倍率不是2的方倍也会合并不成。
2.优先队列。
map迭代法:
#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n;
int x;
int a[N];
int main() {
scanf("%d", &n);
map<long long, long long> b;
int mi = inf;
int ma = -1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] < mi) mi = a[i];
b[a[i]]++;
// if(a[i]>ma)ma=a[i];
}
long long ans = 0;
for (map<long long,long long >::iterator i=b.begin(); n != 1&&i!=b.end(); i ++) {
if (b[i->first] % 2 == 0) {
b[i->first * 2] += b[i->first] / 2;
n -= b[i->first] / 2;
} else {
ans++;
b[i->first * 2] += (b[i->first] + 1) / 2;
n -= (b[i->first] - 1) / 2;
}
}
if(n==1)
printf("%lld\n", ans);
else{
printf("-1\n");
}
return 0;
}
优先队列法:有点类似于哈夫曼编码
#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n;
int a[N];
int main() {
priority_queue<long long, vector<long long>, greater<long long> > p;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
p.push(x);
}
int ans = 0;
int flag = 0;
while (!p.empty()) {
long long a = p.top();
p.pop();
if (p.empty()) {
break;
}
long long b = p.top();
p.pop();
if (a == b) {
p.push(a * 2);
} else {
ans++;
p.push(a * 2);
p.push(b);
}
if (ans > 4e5) {
flag = 1;
break;
}
}
if (flag)
printf("-1\n");
else
printf("%d\n", ans);
return 0;
}