# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1092719 | 2024-09-24T20:02:22 Z | triplem5ds | Sorting (IOI15_sorting) | C++14 | 1 ms | 604 KB |
#include "sorting.h" #include "bits/stdc++.h" using namespace std; int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { vector<pair<int, int>> ans; auto check = [&](int prefix) { ans.clear(); vector<int> vec(N), vis(N, 0), pos(N); copy(S, S + N, vec.begin()); for (int i = 0; i < prefix; i++) swap(vec[X[i]], vec[Y[i]]); for (int i = 0; i < N; i++) { int cur = i; vis[cur] = 1; while (!vis[vec[cur]]) { ans.push_back({ cur,vec[cur] }); cur = vec[cur]; vis[cur] = 1; } } return ans.size() <= prefix; }; int lo = 0, hi = M; while (lo < hi) { int md = lo + (hi - lo) / 2; if (check(md)) hi = md; else lo = md + 1; } check(lo); while (ans.size() < lo) ans.push_back({ 0, 0 }); vector<int> pos(N), vec(N); for (int i = 0; i < N; i++) { pos[i] = vec[i] = i; } for (int i = lo - 1; i >= 0; --i) { P[i] = pos[ans[i].first]; Q[i] = pos[ans[i].second]; swap(pos[vec[X[i]]], pos[vec[Y[i]]]); swap(vec[X[i]], vec[Y[i]]); } return lo; } /* let's say I have an array of pairs(a, b) let's do all swaps of this array at the end of the swaps what I have is the initial position and the final position so I know s[i] will be at the end ss[i] so ideally I want to capture the position of ss[i] and swap i with it now what happens to this system if I do a swap initial doesn't change but capture the position of ss[i] if it's i do swap with val1=ss[i], val2=ss[j] then the final permutation also change so now what I want to swap is pos[ss[j]] and pos[ss[i]] so I swap those as well and just use that along the way */
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Incorrect | 0 ms | 348 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Incorrect | 0 ms | 348 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 344 KB | Output is correct |
2 | Incorrect | 0 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Incorrect | 0 ms | 348 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 604 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 604 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |