Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- main()
- {
- int n, ans = 0;
- cin >> n;
- vector<pair<int, int>> a(n);
- vector<int> p(n, -1), c(1001, -1);
- for(int i = 0; i < n; i++)
- {
- cin >> a[i].first;
- a[i].second = i;
- p[i] = a[i].first;
- c[a[i].first] = i;
- }
- // for(int i = 0; i < n; i++)
- // cout << p[i] << " ";
- // cout << "\n";
- // for(int i = 0; i <= 10; i++)
- // cout << c[i] << " ";
- // cout << "\n";
- sort(a.begin(), a.end());
- // for(auto i : a)
- // cout << i.first << " " << i.second << "\n";
- for(int i = 0; i < n; i++)
- {
- while(i != a[i].second)
- {
- ans += a[a[i].second].first + a[i].first;
- int need = a[a[i].second].first;
- int pos = c[need];
- swap(p[a[i].second], p[pos]);
- a[a[i].second].second = a[i].second;
- c[need] = a[i].second;
- c[a[i].first] = pos;
- a[i].second = pos;
- for(int i = 0; i < n; i++)
- cout << p[i] << " ";
- cout << "\n";
- }
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement