Submission #252296

# Submission time Handle Problem Language Result Execution time Memory
252296 2020-07-25T07:28:52 Z Hehehe Sorting (IOI15_sorting) C++14
100 / 100
414 ms 30496 KB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#include "sorting.h"

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll H = 1e6 + 11;

//ifstream in(".in");
//ofstream out(".out");

int pos[H], n, m;
pi query[H], ans[H], a[H];
/*
bool check(int t, int S[]){

    int in[n + 11], out[n + 11];

    for(int i = 0; i < n; i++){
        in[i] = out[i] = S[i];
    }

    for(int i = 0; i < t; i++){
        swap(out[query[i].ff], out[query[i].ss]);
    }

    bool viz[n + 11];
    memset(viz, 0, sizeof(viz));

    int k = 0;
    for(int i = 0; i < n; i++){

        if(viz[i])continue;

        vector<int>cur;

        int v = out[i];
        while(v != i){
            cur.push_back(v);
            v = out[v];
        }
        cur.push_back(v);

        for(int j = sz(cur) - 1; j > 0; j--){
            a[k++] = {cur[0], cur[j]};
        }

        for(auto it : cur)viz[it] = 1;
    }

    if(k > t)return 0;

    for (int i = 0; i < n; ++i){
        pos[in[i]] = i;
    }

    for(int i = 0; i < k; i++){

        //His move
        int x = query[i].ff, y = query[i].ss;
        swap(in[x], in[y]);
        pos[in[x]] = x;
        pos[in[y]] = y;

        //My move
        x = pos[a[i].ff], y = pos[a[i].ss];
        swap(in[x], in[y]);
        pos[in[x]] = x;
        pos[in[y]] = y;

        ans[i] = {x, y};
    }

    while(k < t)ans[k++] = {0, 0};

    return 1;

} */
bool check(int t, int S[]){

    int out[n], in[n];

    for (int i = 0; i < n; ++i)
        out[i] = in[i] = S[i];

    for (int id = 0; id < t; ++id) {
        int i = query[id].ff, j = query[id].ss;
        swap(out[i], out[j]);
    }

    bool us[n];
    memset(us, false, sizeof us);

    int cnt = 0;

    for (int i = 0; i < n; ++i) {
        if (us[i])
            continue;

        vector<int> ord;
        int v = out[i];
        while (v != i) {
            ord.pb(v);
            us[v] = true;
            v = out[v];
        }
        ord.pb(v);
        us[v] = true;

        for (int j = sz(ord) - 1; j > 0; --j)
            a[cnt++] = mp(ord[0], ord[j]);
    }

    if (cnt > t)return 0;

    for (int i = 0; i < n; ++i)
        pos[in[i]] = i;

    for (int id = 0; id < cnt; ++id) {

        int i = query[id].ff, j = query[id].ss;
        swap(in[i], in[j]);
        pos[in[i]] = i;
        pos[in[j]] = j;


        i = pos[a[id].ff]; j = pos[a[id].ss];
        ans[id] = mp(i, j);
        swap(in[i], in[j]);
        pos[in[i]] = i;
        pos[in[j]] = j;
    }

    while (cnt < t)
        ans[cnt++] = mp(0, 0);

    return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {

    n = N; m = M;

    for(int i = 0; i < m; i++){
        query[i].ff = X[i];
        query[i].ss = Y[i];
    }
    
    int l = -1, r = M;
    while (r - l > 1) {
        int m = (l + r) >> 1;
        if (check(m, S)) {
            r = m;
        } else {
            l = m;
        }
    }
    check(r, S);


    for(int i = 0; i < r; i++){
        P[i] = ans[i].ff;
        Q[i] = ans[i].ss;
    }

    return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:161:13: warning: declaration of 'm' shadows a global declaration [-Wshadow]
         int m = (l + r) >> 1;
             ^
sorting.cpp:22:16: note: shadowed declaration is here
 int pos[H], n, m;
                ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 768 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 2 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 260 ms 27200 KB Output is correct
16 Correct 322 ms 27352 KB Output is correct
17 Correct 318 ms 29536 KB Output is correct
18 Correct 224 ms 24408 KB Output is correct
19 Correct 331 ms 27876 KB Output is correct
20 Correct 343 ms 28348 KB Output is correct
21 Correct 324 ms 28400 KB Output is correct
22 Correct 277 ms 24444 KB Output is correct
23 Correct 303 ms 30496 KB Output is correct
24 Correct 351 ms 30068 KB Output is correct
25 Correct 363 ms 29720 KB Output is correct
26 Correct 345 ms 27404 KB Output is correct
27 Correct 305 ms 26324 KB Output is correct
28 Correct 347 ms 29080 KB Output is correct
29 Correct 412 ms 28180 KB Output is correct
30 Correct 283 ms 25464 KB Output is correct
31 Correct 383 ms 28792 KB Output is correct
32 Correct 319 ms 29000 KB Output is correct
33 Correct 350 ms 30200 KB Output is correct
34 Correct 305 ms 29048 KB Output is correct
35 Correct 335 ms 26880 KB Output is correct
36 Correct 212 ms 23928 KB Output is correct
37 Correct 350 ms 30128 KB Output is correct
38 Correct 414 ms 28792 KB Output is correct
39 Correct 358 ms 29096 KB Output is correct