题目链接: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 }