pratiyush7

Untitled

Nov 23rd, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7.  
  8.  
  9. /*
  10. * Complete the 'maximumClusterQuality' function below.
  11. *
  12. * The function is expected to return a LONG_INTEGER.
  13. * The function accepts following parameters:
  14. * 1. INTEGER_ARRAY speed
  15. * 2. INTEGER_ARRAY reliability
  16. * 3. INTEGER maxMachines
  17. */
  18. #define ll long long int
  19.  
  20. ll maximumClusterQuality(vector<int> s, vector<int> r, int m) {
  21. vector<pair<ll,ll>> v;
  22. int n = s.size();
  23. for(int i=0;i<n;i++)
  24. {
  25. v.push_back({r[i],s[i]});
  26. }
  27. sort(v.begin(),v.end());
  28. reverse(v.begin(),v.end());
  29. ll sum = 0;
  30. ll ans = 0;
  31. multiset<ll> sm;
  32. for(int i=0;i<m;i++)
  33. {
  34. sm.insert(v[i].second);
  35. sum+=v[i].second;
  36. ans = max(sum*v[i].second,ans);
  37. }
  38. for(int i=m;i<n;i++)
  39. {
  40. auto it = sm.begin();
  41. ll x = (*it);
  42. if((x) < v[m].second)
  43. {
  44. sum-=(x);
  45. sm.erase(sm.find(x));
  46. sm.insert(v[m].second);
  47. sum+=v[m].second;
  48. }
  49. ans = max(ans,sum*v[m].first);
  50. }
  51. return ans;
  52. }
  53. int main()
Advertisement
Add Comment
Please, Sign In to add comment