zoukankan      html  css  js  c++  java
  • stl(优先队列操作)

    http://codeforces.com/gym/101911/problem/C

    Recently Monocarp has created his own mini-laboratory!

    The laboratory contains n

    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 7 merge, one bacterium with size 14

    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 n

    bacteria his laboratory contains into exactly one bacterium.

    Input

    The first line contains one integer n

    (1n2105)

    — the number of bacteria Monocarp's laboratory contains.

    The second line contains n

    integers a1,a2,,an (1ai109), where ai is the size of the i

    -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 n

    bacteria his laboratory contains into exactly one bacterium.

    Examples

    Input
    2
    1 4
    Output
    2
    Input
    3
    3 6 9
    Output
    -1
    Input
    7
    1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
    Output
    1

    Note

    In the first example Monocarp should buy one bacterium having size 1

    and one bacterium having size 2. Then Monocarp will have 4 bacteria having sizes [1,4,1,2]. Then two bacteria having sizes 1 can be merged into one having size 2. Then Monocarp will have 3 bacteria having sizes [2,4,2]. Then two bacteria having sizes 2 can be merged into one having size 4. Then Monocarp will have 2 bacteria having sizes [4,4], which can be merged into one having size 8

    .

    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 1000000000

    .

    题意:给一组数 , 将两个相同的数合并(相加),如果没有就加上 。如果最后可以合并成一个数,就输出要加的最少次数。否则输出-1。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <string.h>
    #include <vector>
    #include <map>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    using namespace std;
    typedef long long ll ;
    ll a[200009];
    
    
    int main()
    {
        ll n ;
        while(~scanf("%lld" , &n))
        {
            priority_queue<ll , vector<ll> , greater<ll> >q;
            for(int i = 0 ; i < n ; i++)
            {
                ll x ;
                scanf("%lld" , &x);
                q.push(x);
            }
            ll flag = 0 , ans = 0 ;
            while(!q.empty())
            {
    ll x
    = q.top() ;
           q.pop();
    if(!q.empty()) {
    ll y
    = q.top() ;
    if(x == y)//注意if的顺序 { q.pop() ; q.push(x * 2); } else if(x * 2 > y) { flag = 1 ; break ; } else if(x < y) { q.push(x*2); ans++ ; } } else break ; } if(!flag) cout << ans << endl ; else cout << -1 << endl ; } return 0; }
  • 相关阅读:
    今天刚开通博客,很开心
    ZYNQ双核AMP开发详解(USE_AMP -DUSE_AMP=1 含义和作用详解)
    赛灵思机器学习套件(ML Suite) v1.4 DPU, DNNK
    petalinux add pre-build application to rootfs compile faliure solution
    如何用SDK生成device tree
    Zynq UltraScale+ MPSoC device tree 中 CCF中 clock output数字映射表
    Zynq ZC702平台 QSPI + eMMC实现
    Zynq ZC702平台 Linux + Baremetal AMP实现(一)【快速启动CPU1】
    第五章 任务执行
    第四章 闭锁
  • 原文地址:https://www.cnblogs.com/nonames/p/11391268.html
Copyright © 2011-2022 走看看