Submission #291587

# Submission time Handle Problem Language Result Execution time Memory
291587 2020-09-05T13:59:07 Z MarcoMeijer Sorting (IOI15_sorting) C++14
100 / 100
335 ms 22672 KB
#include <bits/stdc++.h>

using namespace std;

#include "sorting.h"

//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second
#define sz size()

const int MX = 2e5+100;

int a[MX], sa[MX], ss[MX];
int b[MX];
int n;
int *S, *X, *Y;

void swapA(int x, int y) {
    swap(a[x], a[y]);
    sa[a[x]] = x;
    sa[a[y]] = y;
}
void swapS(int x, int y) {
    swap(S[x], S[y]);
    ss[S[x]] = x;
    ss[S[y]] = y;
}

int visited[MX];
void dfs(int u) {
    if(visited[u]) return;
    visited[u] = 1;
    dfs(b[u]);
}
bool possibleSwaps(int swaps) {
    RE(i,n) visited[i] = 0;
    int cyc = 0;
    RE(i,n) {
        if(visited[i]) continue;
        dfs(i);
        cyc++;
    }
    if(n-cyc <= swaps) return 1;
    return 0;
}

bool possible(int am) {
    RE(i,n) a[i]=sa[i]=i;
    RE(i,n) b[i] = S[i];
    RE(i,am) {
        swapA(sa[X[i]], sa[Y[i]]);
        swap(b[X[i]], b[Y[i]]);
    }
    return possibleSwaps(am);
}

int findSwapPairs(int _n, int _S[], int m, int _X[], int _Y[], int P[], int Q[]) {
    n=_n, S=_S, X=_X, Y=_Y;

    int lb=0, ub=n;
    while(lb != ub) {
        int mid=(lb+ub)/2;
        if(possible(mid)) ub=mid;
        else lb=mid+1;
    }
    int times = lb;
    possible(times);

    RE(i,n) ss[S[i]] = i;

    int R = 0; int curI = 0;
    RE(i,times) {
        R++;
        swapA(X[i], Y[i]);
        swapS(X[i], Y[i]);

        int x=0, y=0;
        while(x == y) {
            if(curI == n) break;    
            x = ss[curI];
            y = sa[curI];
            curI++;
        }

        swapS(x, y);

        P[R-1] = x;
        Q[R-1] = y;
    }
    return R;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:74:41: warning: unused parameter 'm' [-Wunused-parameter]
   74 | int findSwapPairs(int _n, int _S[], int m, int _X[], int _Y[], int P[], int Q[]) {
      |                                     ~~~~^
# 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 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 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 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 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 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 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 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 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 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 0 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 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 2 ms 512 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 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 416 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 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 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 416 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 196 ms 12408 KB Output is correct
16 Correct 244 ms 20472 KB Output is correct
17 Correct 312 ms 21664 KB Output is correct
18 Correct 77 ms 17148 KB Output is correct
19 Correct 218 ms 19960 KB Output is correct
20 Correct 243 ms 20728 KB Output is correct
21 Correct 247 ms 20728 KB Output is correct
22 Correct 188 ms 17016 KB Output is correct
23 Correct 207 ms 22520 KB Output is correct
24 Correct 326 ms 22140 KB Output is correct
25 Correct 314 ms 21880 KB Output is correct
26 Correct 219 ms 20600 KB Output is correct
27 Correct 197 ms 19960 KB Output is correct
28 Correct 322 ms 21880 KB Output is correct
29 Correct 281 ms 21496 KB Output is correct
30 Correct 142 ms 19320 KB Output is correct
31 Correct 299 ms 21880 KB Output is correct
32 Correct 222 ms 21624 KB Output is correct
33 Correct 306 ms 22136 KB Output is correct
34 Correct 257 ms 22008 KB Output is correct
35 Correct 219 ms 19832 KB Output is correct
36 Correct 63 ms 18296 KB Output is correct
37 Correct 330 ms 22672 KB Output is correct
38 Correct 313 ms 21752 KB Output is correct
39 Correct 335 ms 22008 KB Output is correct