From 2293bcdc2bd93b3de47ed7a4384503ac0c3aa32a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 23 May 2022 23:41:27 +0800 Subject: [PATCH] C - Double Sort https://codeforces.com/contest/1681/submission/158201208 --- Codeforces/1681/C/C.cpp | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Codeforces/1681/C/C.cpp diff --git a/Codeforces/1681/C/C.cpp b/Codeforces/1681/C/C.cpp new file mode 100644 index 00000000..6629ef0a --- /dev/null +++ b/Codeforces/1681/C/C.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105; + +int t, n, a[N], b[N]; +std::pair c[N]; +std::vector> nums, ans; + +int main() { + std::ios::sync_with_stdio(false); + + cin >> t; + + while (t--) { + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (int i = 1; i <= n; i++) { + cin >> b[i]; + } + + for (int i = 1; i <= n; i++) { + nums.push_back(std::make_pair(a[i], b[i])); + c[i] = std::make_pair(a[i], b[i]); + } + + std::sort(nums.begin(), nums.end()); + + bool flag = true; + for (int i = 1; i < nums.size(); i++) { + if (nums[i - 1].second > nums[i].second) { + cout << -1 << endl; + flag = false; + break; + } + } + + if (flag) { + for (int i = 1; i < n; i++) { + for (int j = i + 1; j <= n; j++) { + if (c[i] > c[j]) { + ans.push_back(std::make_pair(i, j)); + std::swap(c[i], c[j]); + } + } + } + + cout << ans.size() << endl; + + for (auto e : ans) { + cout << e.first << ' ' << e.second << endl; + } + } + + nums.clear(); + ans.clear(); + } + + return 0; +}