From 8530e20eb0aa27f2fe29bf71c600f3d269e39dfc Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 18 Nov 2022 13:56:14 +0800 Subject: [PATCH] #3631. Swap http://www.nfls.com.cn:20035/submission/591497 --- NFLSOJ/0/3631/3631.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 NFLSOJ/0/3631/3631.cpp diff --git a/NFLSOJ/0/3631/3631.cpp b/NFLSOJ/0/3631/3631.cpp new file mode 100644 index 00000000..8980c922 --- /dev/null +++ b/NFLSOJ/0/3631/3631.cpp @@ -0,0 +1,46 @@ +// #include +#include +#include +#include +#include + +// 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> 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; +}