Gym - 101911C Bacteria (數學規律+優先隊列)

C. Bacteria
time limit per test2.0 s
memory limit per test256 MB
inputstandard input
outputstandard output
Recently Monocarp has created his own mini-laboratory!
The laboratory contains
n
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
7
merge, one bacterium with size
14
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
n
bacteria his laboratory contains into exactly one bacterium.
Input
The first line contains one integer
n
n
(1≤n≤2⋅
10
5
)
(1≤n≤2⋅105)
— the number of bacteria Monocarp’s laboratory contains.
The second line contains
n
n
integers
a
1
,
a
2
,…,
a
n
a1,a2,…,an
(1≤
a
i

10
9
)
(1≤ai≤109)
, where
a
i
ai
is the size of the
i
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
n
bacteria his laboratory contains into exactly one bacterium.
Examples
Input
Copy
2
1 4
Output
Copy
2
Input
Copy
3
3 6 9
Output
Copy
-1
Input
Copy
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
Copy
1
Note
In the first example Monocarp should buy one bacterium having size
1
1
and one bacterium having size
2
2
. Then Monocarp will have
4
4
bacteria having sizes
[1,4,1,2]
[1,4,1,2]
. Then two bacteria having sizes
1
1
can be merged into one having size
2
2
. Then Monocarp will have
3
3
bacteria having sizes
[2,4,2]
[2,4,2]
. Then two bacteria having sizes
2
2
can be merged into one having size
4
4
. Then Monocarp will have
2
2
bacteria having sizes
[4,4]
[4,4]
, which can be merged into one having size
8
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
1000000000
.
問題鏈接: http://codeforces.com/gym/101911/problem/C
問題簡述: 給定n個細胞以及每個細胞的大小,相同的細胞能進行融合,如果能融合到只剩1個細胞則輸出需要購買多少細胞才能融合到剩下一個細胞。如果不能則輸出-1
問題分析: 先判斷他們能不能融合剩下一個細胞(一直除與2直到不能再分爲止,要是出現兩種情況則直接輸出-1),如果能,再利用優先隊列進行模擬融合過程即可。(我之前一直以爲優先隊列是從小到大排序,改了1個小時一直改不出答案…長個教訓)
AC通過的C++語言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#define ll long long
#define endl '\n'
#include<ctime>
using namespace std;

double a[300005];
ll b[300005];
priority_queue<ll,vector<ll>,greater<ll> > q;//優先隊列從小到大排序

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    cin>>a[0];
    b[0]=(ll)a[0];
    q.push(b[0]);
    double g=a[0],c=a[0];
    while(c/2==(int)(c/2))//如果能再分
    {
        g=c/2;找出第一種情況
        c/=2;
    }
    int ans=0;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        c=a[i];
        double p=a[i];
        while(c/2==(int)(c/2))
        {
            p=c/2;
            c/=2;
        }
        if(g!=p)//如果與第一種情況不同,則直接輸出-1
        {
            ans=-1;
            break;
        }
        b[i]=(ll)a[i];//強制轉成long long
        q.push(b[i]);//入隊
    }
    if(ans!=-1)
    {
        while(q.size()>1)//如果有兩個細胞以上
        {
            ll k=q.top();
            q.pop();
            ll z=q.top();
            q.pop();
            if(z==k)//現有細胞大小相等的情況
            {
                z*=2;
                q.push(z);
            }
            else//不等的情況
            {
                q.push(z);
                ans++;
                q.push(k*2);
            }
        }
    }
    cout<<ans;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章