Untitled
unknown
plain_text
12 days ago
890 B
4
Indexable
#include <bits/stdc++.h> #define Task "Task" #define maxn 100005 using namespace std; int n,a[maxn],b[maxn],pos[maxn]; bool mark[maxn]; vector<pair<int,int>> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(Task".inp","r")) { freopen(Task".inp","r",stdin); freopen(Task".out","w",stdout); } cin >> n; for(int i=1;i<=n;i++) cin >> a[i],b[i] = a[i]; sort(b+1,b+n+1); for(int i=1;i<=n;i++) { a[i] = lower_bound(b+1,b+n+1,a[i])-b; while (mark[a[i]]) a[i]++; mark[a[i]]=1; } for(int i=1;i<=n;i++) { while (a[i]!=i) { res.push_back({min(i,a[i]),max(i,a[i])}); swap(a[i],a[a[i]]); } } for(auto [i,j] : res) cout << i << ' ' << j << '\n'; //for(int i=1;i<=n;i++) cout << a[i] << " " ; return 0; }
Editor is loading...
Leave a Comment