fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. vector<int> arr={5,4,3,2,1};
  6. vector<pair<int,int>> v;
  7. int step=0;
  8. int n=arr.size();
  9. map<int,int>ok;
  10. for(int i=0;i<n;i++){
  11. ok[arr[i]]++;
  12. }
  13. for(auto it=ok.begin();it!=ok.end();it++){
  14. v.push_back({it->first,it->second});
  15. }
  16. for(int i=v.size()-1;i>0;i--){
  17. // cout<<v[i].first<<" "<<v[i].second<<endl;
  18. step+=v[i].second;
  19. v[i-1].second+=v[i].second;
  20. v[i].second=0;
  21. }
  22. for(int i=v.size()-1;i>=0;i--){
  23. cout<<v[i].first<<" "<<v[i].second<<endl;
  24. }
  25. cout<<step;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
5  0
4  0
3  0
2  0
1  5
10