fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. // index 1 2 3 4 5 6 7 8 9
  7. int b[9] = {5 , 5 , 5 , 3 , 3 , 2 , 2 , 1 , 1};
  8. int n = 9;
  9.  
  10. b[n+1] = 0; // 1 - based indexing
  11. /*
  12.   Element , frequency
  13.   5 3
  14.   3 2
  15.   2 2
  16.   1 2
  17.   */
  18. unordered_map<int,int>k;
  19. for(int i=1; i<=n; i++)
  20. {
  21. k[b[i]] = k[b[i]] + 1;
  22. }
  23.  
  24. /*
  25.   <(5,3)<5 , 3>>g; // g(5,3)
  26.   <(3,2)<3 , 2>g; // g(3,2)
  27.   <(2,2)<2 , 2>g; // g(2,2)
  28.   <(1,2)<1 , 2>g; // g(1.2)
  29.  
  30.   */
  31. vector<pair<int,int>>g;
  32. for(auto u=k.begin(); u!=k.end(); u++) // u is an iterator
  33. {
  34.  
  35. // Element Frequency
  36. g.push_back({u->first, u->second}); // to access 'u' . dot operator we not arrow -> symbol we use to acess it
  37. }
  38.  
  39. int size = g.size(); // map size
  40. int step = 0;
  41.  
  42. for(int i=size - 1; i>=1; i--)
  43. {
  44. g[i-1].second = g[i-1].second + g[i].second;
  45. step = step + g[i].second;
  46. g[i].second = 0;
  47. }
  48. cout<<step<<endl;
  49.  
  50. return 0;
  51. }
Runtime error #stdin #stdout #stderr 0s 5320KB
stdin
Standard input is empty
stdout
20
stderr
*** stack smashing detected ***: <unknown> terminated