Bacteria(优先队列)
题目链接:http://codeforces.com/gym/101911/problem/C
问题简述:给定n个细胞以及每个细胞的大小,相同的细胞能进行融合,如果能融合到只剩1个细胞则输出需要额外增加多少细胞才能把全部融合到只剩一个细胞,如果不能则输出-1.
问题分析:每融合一次都要排序,所以用优先队列。
1 /* */ 2 # include <iostream> 3 # include <stdio.h> 4 # include <string.h> 5 # include <string> 6 # include <cstdlib> 7 # include <cmath> 8 # include <climits> 9 # include <cctype> 10 # include <iomanip> 11 # include <cassert> 12 # include <deque> 13 # include <queue> 14 # include <stack> 15 # include <vector> 16 # include <utility> 17 # include <list> 18 # include <set> 19 # include <map> 20 # include <functional> 21 # include <ctime> 22 # include <algorithm> 23 using namespace std; 24 typedef long long ll; 25 const double pi=acos(-1.0); 26 const int maxn=1e6; 27 const ll mod=1e9+7; 28 # define lson l,m,rt<<1 29 # define rson m+1,r,rt<<1|1 30 # define lowbit(x)(x&(-x)) 31 # define lcm(a,b)(a*b/__gcd(a,b)) 32 33 int main() 34 { 35 priority_queue<ll, vector<ll>, greater<ll> >q; 36 ll a, x, k=1, ans=0; 37 cin>>a; 38 for( ll i=1; i<=a; i++ ) 39 { 40 cin>>x; 41 q.push(x); 42 } 43 44 while( !q.empty()) 45 { 46 ll t=q.top(); 47 q.pop(); 48 if( !q.empty()) 49 { 50 ll g=q.top(); 51 if( t==g ) 52 { 53 t = t+g; 54 q.pop(); 55 q.push(t); 56 } 57 else 58 { 59 t=2*t; 60 q.push(t); 61 ans++; 62 } 63 } 64 else 65 break; 66 if( ans>4e5 )///加了这么多还没有合成一个说明不能合成一个了 67 { 68 k=0; 69 break; 70 } 71 } 72 if( k==1 ) 73 cout<<ans<<endl; 74 else 75 cout<<-1<<endl; 76 }
转载于:https://www.cnblogs.com/wsy107316/p/11373003.html
Bacteria(优先队列)相关推荐
- C - Bacteria(优先队列)
C - Bacteriahttps://vjudge.csgrandeur.cn/problem/Gym-101911C问题简述:给定n个细胞以及每个细胞的大小,相同的细胞能进行融合,如果能融合到只剩 ...
- 优先队列的应用 C++实现
优先队列的应用 C++实现 优先队列可以用堆来实现, 堆底层可以用数组表示, 通过索引关系,可以表示成一颗二叉完全树 C++的STL提供了相应的容器适配器 包含在queue头文件中 下面通过一道题来看 ...
- ccf 交通规划(迪杰斯特拉优先队列模板)
什么跟什么就是刘汝佳小白书迪杰斯特拉队列的优先队列法 #include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f ...
- c++ std::priority_queue优先队列
template <class T, class Container = vector<T>,class Compare = less<typename Container:: ...
- 《程序设计解题策略》——1.6 利用左偏树实现优先队列的合并
本节书摘来自华章计算机<程序设计解题策略>一书中的第1章,第1.6节,作者:吴永辉 王建德 更多章节内容可以访问云栖社区"华章计算机"公众号查看. 1.6 利用左偏树实 ...
- 排序(一)归并、快排、优先队列等(图文具体解释)
排序(一) 0基础排序算法 选择排序 思想:首先,找到数组中最小的那个元素.其次,将它和数组的第一个元素交换位置.再次.在剩下的元素中找到最小的元素.将它与数组的第二个元素交换位置. 如此往复,直到将 ...
- MYSQL实现ORDER BY LIMIT的方法以及优先队列(堆排序)
一.MYSQL中的LIMIT和ORACLE中的分页 在MYSQL官方文档中描述limit是在结果集中返回你需要的数据,它可以尽快的返回需要的行而不用管剩下的行, 在ORACLE中也有相关的语法比如 1 ...
- 373. Find K Pairs with Smallest Sums (java,优先队列)
题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...
- 暑期集训4:栈,树,优先队列 例 : UVA - 514
2018学校暑期集训第四天--栈,树,优先队列 例题 -- UVA - 514 Rails There is a famous railway station in PopPush City. ...
最新文章
- Auto ML自动调参
- redis服务器索引文件删除,Redis基本命令整理
- blender 子弹时间 动画
- python2.7获取当前脚本目录
- 文本挖掘之文本相似度判定
- sqlite的数据导入 导出
- 由塔科马大桥事件 谈软件架构
- 让CentOS能用yum自动安装rar和unrar
- php如何抓取一行的内容,提取一行作为对象 - PHP 7 中文文档
- oracle数字类型ef映射,Entity Framework 学习中级篇5—使EF支持Oracle9i - ♂风车车.Net - 博客园...
- RN学习笔记02:利用WebStorm创建RN项目
- mysql2个字段还会map_通过注解实现MyBatis将sql查询结果的两个字段分别作为map的key,value...
- 分层模式下的Lazy Load ——探索Domain Model系列(下)--转
- JAVA中的文件及目录处理类--File
- php_curl-5.4.3-VC9-x64下php_curl.dll加载出错
- Unity给力插件之ShaderForge
- python版武侠小说男女侠姓名生成器
- 如何用excel筛选相似内容_如何excel中筛选两个表中相同的数据
- Java 接口 及其在 Minecraft mod中相关应用
- Chelly个人训练