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
(1≤n≤2⋅105)
— the number of bacteria Monocarp's laboratory contains.
The second line contains n
integers a1,a2,…,an (1≤ai≤109), 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
2
1 4
2
3
3 6 9
-1
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
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[]; int main()
{
ll n ;
while(~scanf("%lld" , &n))
{
priority_queue<ll , vector<ll> , greater<ll> >q;
for(int i = ; i < n ; i++)
{
ll x ;
scanf("%lld" , &x);
q.push(x);
}
ll flag = , ans = ;
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 * );
}
else if(x * > y)
{
flag = ;
break ;
}
else if(x < y)
{
q.push(x*);
ans++ ;
}
}
else
break ; }
if(!flag)
cout << ans << endl ;
else
cout << - << endl ; } return ;
}
stl(优先队列操作)的更多相关文章
- 基于STL优先队列和邻接表的dijkstra算法
首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...
- STL 常见操作
stl的操作不是很熟练, 记录一下 1.vector: 排序: sort(vc.begin(),vc.end()); 去重: sort(vc.begin(),vc.end()); num.erase( ...
- CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆)
CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆) Description 有n个函数,分别为F1,F2,...,Fn.定义 \(Fi(x)=Aix^2+Bix ...
- C++ STL堆操作
/* STL 最大堆.最小堆的应用 */ #include <iostream> #include <vector> #include <algorithm> // ...
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- 合并果子(STL优先队列)
STL优先队列:priority_queue 定义:priority_queue<int>q; 从小到大:priority_queue<int,vector<int>,g ...
- STL 优先队列
STL 栈,队列,优先队列用法 分类: Learning C++2013-11-15 00:52 843人阅读 评论(2) 收藏 举报 c++栈队列优先队列STL STL 中栈的使用方法(stack) ...
随机推荐
- vue.js(19)--vue中子组件调用父组件的方法
子组件是不能直接使用父组件中的方法的,需要进行事件绑定(v-on:自定义方法名="父组件方法名"),然后在子组件中再定义一个方法,使用this.$emit('自定义方法名')语句完 ...
- 狼人杀校园升级版:学霸大战学渣 Who is the king of examination!
之前在微博上看到一个很老的段子 写道 天黑请闭眼.学霸请睁眼,学霸请答题,好的学霸请闭眼:学渣请睁眼,学渣请坐弊,好的学渣请闭眼:监考老师请睁眼,监考老师请确定坐弊考生,监考老师请统一意见,好的监考老 ...
- openprocess提升为测试权限
BOOL EnableDebugPrivilege() { HANDLE hToken; BOOL fOk=FALSE; if(OpenProcessToken(GetCurrentProcess() ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- vue2.0 之 nextTick
Vue.nextTick 将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新. 它跟实例方法vm.$nextTick() 一样,不同的是 this 自动绑定到 ...
- luogu4061 大吉大利,晚上吃鸡!
链接 最短路径\(dag\),一道好题. 题目大意:求一张图中满足下列要求的点对\((i,j)\)数量: 所有最短路径必定会经过 \(i\) 点和 \(j\) 点中的任意一点. 不存在一条最短路同时经 ...
- docker设置proxy
该方法是持久化的,修改后会一直生效.该方法覆盖了默认的docker.service文件. 1. 为docker服务创建一个内嵌的systemd目录 mkdir -p /etc/systemd/syst ...
- rocketmq架构设计
# 架构设计 1 技术架构 RocketMQ架构上主要分为四部分,如上图所示: Producer:消息发布的角色,支持分布式集群方式部署.Producer通过MQ的负载均衡模块选择相应的Broker集 ...
- SpringCLoud之搭建Zuul网关集群
1.使用技术 Springboot,SpringCloud,Zuul,Nignx 2.目的 使用Zuul搭建微服务高可用的网关 3.项目创建 3.1 创建注册中心(略) 3.2 创建一个hello-s ...
- 饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点
编者按:今年4月,饿了么正式加入了阿里新零售战队,进一步加速其在本地生活市场的扩张速度.在创业9年的时间中,饿了么在外卖领域经历了真正的“从0到1”,尤其是在外卖平台的技术升级方面,越过了一个又一个的 ...