Tourist Code
What I do not understand is how after finding array b we get the correct answer.
Thanks in advance.
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
/**
* author: tourist
* created: 04.07.2020 18:34:48
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
iota(b.begin(), b.end(), 0);
sort(b.begin(), b.end(), [&](int i, int j) {
if (a[i] != a[j]) {
return a[i] < a[j];
}
return i < j;
});
vector<pair<int, int>> ret;
for (int it = 0; it < n; it++) {
for (int i = 0; i < n - 1; i++) {
if (b[i] > b[i + 1]) {
ret.emplace_back(b[i + 1], b[i]);
swap(b[i], b[i + 1]);
}
}
}
cout << ret.size() << '\n';
for (auto& p : ret) {
cout << p.first + 1 << " " << p.second + 1 << '\n';
}
return 0;
}
What I do not understand is how after finding array b we get the correct answer.
Thanks in advance.
Name |
---|
Auto comment: topic has been updated by Rock2000 (previous revision, new revision, compare).