Submission #286753

# Submission time Handle Problem Language Result Execution time Memory
286753 2020-08-30T20:54:02 Z Ozy Sorting (IOI15_sorting) C++17
100 / 100
191 ms 26616 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define debug(a) cout << #a << " = " << a << endl

int sum,largo,act,cont;
int seg,n,m,w,pas;
int s[200001],x[600001],y[600001],doble[200001],visitados[200001],id[200001];

bool checa(int pos) {

    rep(i,0,n-1) visitados[i] = 0;
    rep(i,0,n-1) doble[i] = s[i];
    rep(i,0,pos) swap(doble[x[i]],doble[y[i]]);

    sum = 0;
    rep(i,0,n-1) {
        if (visitados[i] == 0) {

            act = doble[i];
            largo = 0;
            visitados[i] = 1;

            while (act != i) {
                largo++;
                visitados[act] = 1;
                act = doble[act];
            }

            sum += largo;

        }
    }

    if (sum <= (pos+1)) return true;
    else return false;

}

void binaria(int ini, int fin) {
    int mitad;

    if (ini <= fin) {
        mitad = (ini+fin)/2;
        if (checa(mitad)) {
            seg = mitad;
            binaria(ini,mitad-1);
        }
        else binaria(mitad+1 ,fin);

    }
}

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

    rep(i,0,N-1) if (S[i] == i) cont++;
    
    if (cont == N) return 0;
    
    n = N;
    m = M;
    rep(i,0,N-1) s[i] = S[i];
    rep(i,0,M-1) x[i] = X[i];
    rep(i,0,M-1) y[i] = Y[i];

    binaria(0,M-1);

    rep(i,0,n-1) visitados[i] = 0;
    rep(i,0,n-1) doble[i] = s[i];
    rep(i,0,seg) swap(doble[x[i]],doble[y[i]]);

    //rep(i,0,n-1) cout << s[i] << ' ';
    //cout << endl;
    //rep(i,0,n-1) cout << doble[i] << ' ';
    //cout << endl;

    rep(i,0,n-1) id[doble[i]] = i;
    cont = 0;
    rep(i,0,n-1) {
        if (visitados[i] == 0) {

            act = doble[i];
            pas = doble[act];
            visitados[i] = 1;

            while (visitados[id[pas]] == 0) {

                P[cont] = act;
                Q[cont] = pas;
                cont++;

                visitados[id[pas]] = 1;
                act = pas;
                pas = doble[pas];
            }

        }
    }
    //debug(cont);
    //debug(seg);
    while (cont <= seg) {
        P[cont] = 0;
        Q[cont] = 0;
        cont++;
    }

    rep(i,0,n-1) id[s[i]] = i;
    rep(i,0,seg) {

        swap(id[ s[ x[i] ] ],id[ s[ y[i] ] ]);
        swap(s[x[i]], s[y[i]] );

        swap(s[ id [ P[i]] ], s[ id [Q[i]] ]);
        swap(id[ P[i] ], id[ Q[i] ]);
        P[i] = id[P[i]];
        Q[i] = id[Q[i]];

    }

    //rep(i,0,seg) cout << P[i] << ' ' << Q[i] << endl;

    seg++;
    //cerr << seg;
    return seg;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 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 1 ms 256 KB Output is correct
2 Correct 0 ms 256 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 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 256 KB Output is correct
2 Correct 0 ms 256 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 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 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 1 ms 384 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 2 ms 640 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 1 ms 768 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 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 1 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 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 1 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 156 ms 22648 KB Output is correct
16 Correct 166 ms 23160 KB Output is correct
17 Correct 183 ms 24952 KB Output is correct
18 Correct 44 ms 13432 KB Output is correct
19 Correct 146 ms 23804 KB Output is correct
20 Correct 155 ms 24568 KB Output is correct
21 Correct 157 ms 24568 KB Output is correct
22 Correct 147 ms 19896 KB Output is correct
23 Correct 169 ms 25336 KB Output is correct
24 Correct 183 ms 25208 KB Output is correct
25 Correct 185 ms 25464 KB Output is correct
26 Correct 148 ms 24440 KB Output is correct
27 Correct 133 ms 23672 KB Output is correct
28 Correct 178 ms 24952 KB Output is correct
29 Correct 173 ms 24952 KB Output is correct
30 Correct 102 ms 23160 KB Output is correct
31 Correct 174 ms 25592 KB Output is correct
32 Correct 168 ms 24440 KB Output is correct
33 Correct 182 ms 25464 KB Output is correct
34 Correct 175 ms 25212 KB Output is correct
35 Correct 148 ms 23544 KB Output is correct
36 Correct 61 ms 22264 KB Output is correct
37 Correct 191 ms 26616 KB Output is correct
38 Correct 181 ms 25168 KB Output is correct
39 Correct 181 ms 25708 KB Output is correct