Submission #572538

# Submission time Handle Problem Language Result Execution time Memory
572538 2022-06-04T16:30:23 Z MohamedFaresNebili Sorting (IOI15_sorting) C++14
100 / 100
248 ms 27480 KB
#include <bits/stdc++.h>
#include "sorting.h"
#include <ext/pb_ds/assoc_container.hpp>

        using namespace std;
        using namespace __gnu_pbds;

        using ll = long long;
        using ii = pair<int, int>;
        using vi = vector<int>;

        #define pb push_back
        #define pp pop_back
        #define ff first
        #define ss second
        #define lb lower_bound
        #define all(x) (x).begin(), (x).end()

        typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update> indexed_set;

        vector<ii> ans, ve;
        bool can(int N, int S[], int M, int X[], int Y[]) {
            ve.clear();
            int s[N], pos[N], res[N], arr[N], a[N], x[M], y[M];
            for(int l = 0; l < N; l++) {
                res[l] = l, arr[l] = l; a[l] = l;
                s[l] = S[l]; pos[s[l]] = l;
            }
            for(int l = 0; l < M; l++) {
                x[l] = X[l], y[l] = Y[l];
                swap(res[x[l]], res[y[l]]);
            }
            int c = 0;
            for(int l = 0; l < N; l++) {
                if(res[l] == pos[l]) continue;
                int i = X[c], j = Y[c];
                swap(arr[i], arr[j]);
                swap(a[arr[i]], a[arr[j]]);

                ve.pb({a[pos[l]], a[res[l]]});
                int val = s[res[l]];
                swap(s[pos[l]], s[res[l]]);
                swap(pos[l], pos[val]);
                c++;
            }
            if(c > M) return 0;
            for(int l = c; l < M; l++) ve.pb({0, 0});
            return 1;
        }

        int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
            int lo = 0, hi = N - 1; M = N; ans.clear();
            while(lo <= hi) {
                int md = (lo + hi) / 2;
                if(can(N, S, md, X, Y)) {
                    M = md; hi = md - 1;
                    ans.assign(all(ve));
                }
                else lo = md + 1;
            }
            int l = 0;
            for(auto u : ans) P[l] = u.ff, Q[l++] = u.ss;
            return M;
        }
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 436 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 436 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 436 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 164 ms 17472 KB Output is correct
16 Correct 174 ms 24836 KB Output is correct
17 Correct 226 ms 26132 KB Output is correct
18 Correct 65 ms 19156 KB Output is correct
19 Correct 152 ms 22260 KB Output is correct
20 Correct 158 ms 24212 KB Output is correct
21 Correct 181 ms 24300 KB Output is correct
22 Correct 139 ms 21572 KB Output is correct
23 Correct 163 ms 27068 KB Output is correct
24 Correct 229 ms 26716 KB Output is correct
25 Correct 237 ms 26316 KB Output is correct
26 Correct 164 ms 23428 KB Output is correct
27 Correct 146 ms 22424 KB Output is correct
28 Correct 223 ms 26188 KB Output is correct
29 Correct 200 ms 25412 KB Output is correct
30 Correct 116 ms 21592 KB Output is correct
31 Correct 219 ms 26072 KB Output is correct
32 Correct 174 ms 26076 KB Output is correct
33 Correct 228 ms 26680 KB Output is correct
34 Correct 184 ms 26608 KB Output is correct
35 Correct 161 ms 22032 KB Output is correct
36 Correct 56 ms 20592 KB Output is correct
37 Correct 231 ms 27480 KB Output is correct
38 Correct 212 ms 26252 KB Output is correct
39 Correct 248 ms 26524 KB Output is correct