Submission #552912

# Submission time Handle Problem Language Result Execution time Memory
552912 2022-04-24T09:44:01 Z elazarkoren Sorting (IOI15_sorting) C++17
100 / 100
410 ms 21064 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
//#define int ll
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;

const int MAX_N = 2e5 + 5;

int x[MAX_N], y[MAX_N];
int n;

int cnt;

vi Compute(vi s) {
    for (int i = 0; i < cnt; i++) swap(s[x[i]], s[y[i]]);
    return s;
}

bool Ok(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
    cnt = m;
    vi s(n);
    for (int i = 0; i < n; i++) s[i] = S[i], x[i] = X[i], y[i] = Y[i];
    vi s2 = Compute(s);
    vi ind1(n), ind2(n);
    for (int i = 0; i < n; i++) {
        ind1[s[i]] = i;
        ind2[s2[i]] = i;
    }
    int index = 0;
    for (int i = 0; i < cnt; i++) {
        swap(s[x[i]], s[y[i]]);
        ind1[s[x[i]]] = x[i];
        ind1[s[y[i]]] = y[i];
        int v1 = 0, v2 = 0;
        for (; index < n; index++) {
            if (s2[index] == index) continue;
            int j = ind2[index];
            v1 = s2[index], v2 = s2[j];
            swap(s2[index], s2[j]);
            ind2[s2[index]] = index;
            ind2[s2[j]] = j;
            index++;
            break;
        }
        int i1 = ind1[v1], i2 = ind1[v2];
//        for (i1 = 0; s[i1] != v1; i1++);
//        for (i2 = 0; s[i2] != v2; i2++);
        p[i] = i1, q[i] = i2;
        swap(s[p[i]], s[q[i]]);
        ind1[s[i1]] = i1;
        ind1[s[i2]] = i2;
    }
    return is_sorted(all(s));
}

int findSwapPairs(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
    n = N;
    int begin = 0, end = n - 1, mid;
    while (begin < end) {
        mid = (begin + end) >> 1;
        if (Ok(N, S, mid, X, Y, p, q)) {
            end = mid;
        } else begin = mid + 1;
    }
    Ok(N, S, end, X, Y, p, q);
    return end;
}
//8
//3 5 6 2 1 7 0 4
//8
//0 1
//0 1
//0 1
//0 1
//0 1
//0 1
//0 1
//0 1

Compilation message

sorting.cpp: In function 'bool Ok(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:29:13: warning: unused parameter 'N' [-Wunused-parameter]
   29 | bool Ok(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
      |         ~~~~^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:66:39: warning: unused parameter 'm' [-Wunused-parameter]
   66 | int findSwapPairs(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
      |                                   ~~~~^
# 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 0 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 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 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 311 ms 10724 KB Output is correct
16 Correct 319 ms 18812 KB Output is correct
17 Correct 388 ms 19792 KB Output is correct
18 Correct 104 ms 18460 KB Output is correct
19 Correct 242 ms 19864 KB Output is correct
20 Correct 279 ms 20496 KB Output is correct
21 Correct 287 ms 20472 KB Output is correct
22 Correct 328 ms 15212 KB Output is correct
23 Correct 319 ms 20628 KB Output is correct
24 Correct 398 ms 20588 KB Output is correct
25 Correct 382 ms 19984 KB Output is correct
26 Correct 258 ms 20072 KB Output is correct
27 Correct 241 ms 19704 KB Output is correct
28 Correct 388 ms 20356 KB Output is correct
29 Correct 343 ms 19928 KB Output is correct
30 Correct 191 ms 19852 KB Output is correct
31 Correct 408 ms 20388 KB Output is correct
32 Correct 355 ms 19808 KB Output is correct
33 Correct 389 ms 20268 KB Output is correct
34 Correct 360 ms 20160 KB Output is correct
35 Correct 259 ms 19252 KB Output is correct
36 Correct 100 ms 19936 KB Output is correct
37 Correct 410 ms 21064 KB Output is correct
38 Correct 384 ms 20124 KB Output is correct
39 Correct 388 ms 20176 KB Output is correct