Advertisement
Georgiy1108

Untitled

Oct 7th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. main()
  6. {
  7.     int n, ans = 0;
  8.     cin >> n;
  9.     vector<pair<int, int>> a(n);
  10.     vector<int> p(n, -1), c(1001, -1);
  11.     for(int i = 0; i < n; i++)
  12.     {
  13.         cin >> a[i].first;
  14.         a[i].second = i;
  15.         p[i] = a[i].first;
  16.         c[a[i].first] = i;
  17.     }
  18. //  for(int i = 0; i < n; i++)
  19. //      cout << p[i] << " ";
  20. //  cout << "\n";
  21. //  for(int i = 0; i <= 10; i++)
  22. //      cout << c[i] << " ";
  23. //  cout << "\n";
  24.     sort(a.begin(), a.end());
  25. //  for(auto i : a)
  26. //      cout << i.first << " " << i.second << "\n";
  27.     for(int i = 0; i < n; i++)
  28.     {
  29.         while(i != a[i].second)
  30.         {
  31.             ans += a[a[i].second].first + a[i].first;
  32.             int need = a[a[i].second].first;
  33.             int pos = c[need];
  34.             swap(p[a[i].second], p[pos]);
  35.             a[a[i].second].second = a[i].second;
  36.             c[need] = a[i].second;
  37.             c[a[i].first] = pos;
  38.             a[i].second = pos;
  39.             for(int i = 0; i < n; i++)
  40.                 cout << p[i] << " ";
  41.             cout << "\n";
  42.         }  
  43.     }  
  44.     cout << ans;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement