Created
April 29, 2021 12:27
-
-
Save ABHIINAV12/f583a5415567a8e74bc38b025b1bee69 to your computer and use it in GitHub Desktop.
hackerearth. CFRESH. GO CSK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// divide by 2 the maximum element of array, what is the minimum maximum element you can get at end. | |
// question link : https://www.hackerearth.com/problem/algorithm/beautiful-division/ | |
void solve(){ | |
int n,k; cin>>n>>k; | |
bool all_zero=0; | |
mpi mp; f(i,0,n){ | |
int x; cin>>x; ++mp[x]; | |
if(x!=0) all_zero=1; | |
} | |
if(!all_zero){ | |
cout<<"0\n"; | |
return ; | |
} | |
while(1){ | |
auto ip = mp.end(); | |
--ip; | |
pii it=*ip; | |
mp.erase(ip); | |
if(it.ff<=1) break; | |
int fre=it.ss; | |
int num=it.ff; | |
if(num&1){ | |
mp[num/2]+=fre; | |
mp[num/2 + 1]+=fre; | |
}else mp[num/2]+=fre*2; | |
k-=fre; | |
if(k<0) { | |
cout<<num<<"\n"; | |
return ; | |
} | |
} | |
cout<<"1"<<endl; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment