We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>#define lli long long intusingnamespacestd;structgreaters{booloperator()(constlong&a,constlong&b)const{returna>b;}};intmain(intargc,char*argv[]){ios_base::sync_with_stdio(false);cin.tie(NULL);vector<int>v;llians=0;llin,k;cin>>n>>k;while(n--){llix;cin>>x;v.push_back(x);}// min heapmake_heap(v.begin(),v.end(),greaters());// check if there's a minimum element < kif(v.front()<k){while(v.size()>1){intfirstMinVal=v.front();// delete min heappop_heap(v.begin(),v.end(),greaters());v.pop_back();intsecondMinVal=v.front();// delete min heappop_heap(v.begin(),v.end(),greaters());v.pop_back();intformula=firstMinVal+(2*secondMinVal);// push heapv.push_back(formula);push_heap(v.begin(),v.end(),greaters());ans++;intminVal=v.front();if(minVal>=k){break;}}}if(v.size()==1&&v.front()<k){cout<<-1<<"\n";}else{cout<<ans<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
I am using min heap to solve this problem