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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值