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