mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-09 17:18:49 +00:00
parent
9feffbec47
commit
8530e20eb0
46
NFLSOJ/0/3631/3631.cpp
Normal file
46
NFLSOJ/0/3631/3631.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
// #include <iostream>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// using std::cin;
|
||||
// using std::cout;
|
||||
std::ifstream cin("swap.in");
|
||||
std::ofstream cout("swap.out");
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e5 + 5;
|
||||
|
||||
int n, a[N], p[N];
|
||||
std::vector<std::pair<int, int>> ans;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i];
|
||||
|
||||
p[a[i]] = i;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (a[i] != i) {
|
||||
int x = i, y = p[i];
|
||||
ans.emplace_back(p[x], p[y]);
|
||||
std::swap(a[x], a[y]);
|
||||
p[a[x]] = x, p[a[y]] = y;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans.size() << endl;
|
||||
|
||||
for (auto e : ans) {
|
||||
cout << e.first << ' ' << e.second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user