C - Bacteria

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

InputcopyOutputcopy
2
1 4
2

Sample 2

InputcopyOutputcopy
3
3 6 9
-1

Sample 3

InputcopyOutputcopy
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monster_six

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

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

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

打赏作者

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

抵扣说明:

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

余额充值