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.
/* Sample program illustrating input/output */#include<iostream>#include <vector>#include <cmath>#include <algorithm>usingnamespacestd;intmain(void){intN,K;cin>>N>>K;vector<int>C(N);for(inti=0;i<N;i++){cin>>C[i];}/* * The way I understand it, the "greedy" applies to the florist, not to the algorithm used to * solve the challenge. * My solution presented here passed. * The algorithm to minimize cost for your group to purchase N flowers: * 1. Buy the first K most expensive flowers at their lowest prices each (which is 1 X c_i.) * 2. Buy the second K most expensive flowers at 2 X c_i each * 3. ... * 4. Buy the last (i.e. the m) K most expensive flowers at m X c_i each. */sort(C.begin(),C.end(),greater<int>());intresult=0,m;for(inti=0;i<N;i++){m=floor(i/K)+1;result+=C[i]*m;}cout<<result<<std::endl;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Greedy Florist
You are viewing a single comment's thread. Return to all comments →