Submission #292874

# Submission time Handle Problem Language Result Execution time Memory
292874 2020-09-07T14:30:18 Z alexandra_udristoiu Sorting (IOI15_sorting) C++14
100 / 100
338 ms 25720 KB
#include "sorting.h"
#include<algorithm>
#define DIM 200005
#define f first
#define s second
using namespace std;
static int n, m, v[DIM], p[DIM], ind[DIM], viz[DIM], pos[DIM];
static pair<int, int> sol[DIM], w[DIM];
static void swappos(int x, int y){
    swap(p[x], p[y]);
    pos[ p[x] ] = x;
    pos[ p[y] ] = y;
}
static int solve(int k){
    int i, nr = 0, x;
    for(i = 1; i <= n; i++){
        ind[i] = i;
        viz[i] = 0;
    }
    for(i = 1; i <= k; i++){
        swap(ind[ w[i].f ], ind[ w[i].s ]);
    }
    for(i = 1; i <= n; i++){
        p[i] = v[ ind[i] ];
    }
    for(i = 1; i <= n; i++){
        if(viz[i] == 0){
            viz[i] = 1;
            x = i;
            while(viz[ p[x] ] == 0){
                sol[++nr] = make_pair(p[x], p[ p[x] ]);
                x = p[x];
                viz[x] = 1;
            }
        }
    }
    if(nr <= k){
        for(i = 1; i <= n; i++){
            p[i] = v[i];
            pos[ p[i] ] = i;
        }
        for(i = 1; i <= nr; i++){
            swappos(w[i].f, w[i].s);
            sol[i].f = pos[ sol[i].f ];
            sol[i].s = pos[ sol[i].s ];
            swappos(sol[i].f, sol[i].s);
        }
        for(; i <= k; i++){
            sol[i] = make_pair(1, 1);
        }
        return 1;
    }
    return 0;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int i, st, dr, mid;
    n = N;
    m = M;
    for(i = 1; i <= n; i++){
        v[i] = S[i - 1] + 1;
    }
    for(i = 1; i <= m; i++){
        w[i] = make_pair(X[i - 1] + 1, Y[i - 1] + 1);
    }
    st = 0;
    dr = m;
    while(st <= dr){
        mid = (st + dr) / 2;
        if( solve(mid) ){
            dr = mid - 1;
        }
        else{
            st = mid + 1;
        }
    }
    solve(st);
    for(i = 0; i < st; i++){
        P[i] = sol[i + 1].f - 1;
        Q[i] = sol[i + 1].s - 1;
    }
    return st;
}
# 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 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
# 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 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 0 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 1 ms 384 KB Output is correct
21 Correct 1 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 1 ms 768 KB Output is correct
25 Correct 1 ms 768 KB Output is correct
26 Correct 2 ms 760 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 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 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 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 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 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 1 ms 512 KB Output is correct
15 Correct 250 ms 22392 KB Output is correct
16 Correct 295 ms 22904 KB Output is correct
17 Correct 290 ms 24444 KB Output is correct
18 Correct 117 ms 20216 KB Output is correct
19 Correct 225 ms 23288 KB Output is correct
20 Correct 249 ms 23932 KB Output is correct
21 Correct 221 ms 23804 KB Output is correct
22 Correct 239 ms 19192 KB Output is correct
23 Correct 295 ms 24760 KB Output is correct
24 Correct 319 ms 24572 KB Output is correct
25 Correct 338 ms 24700 KB Output is correct
26 Correct 230 ms 23628 KB Output is correct
27 Correct 205 ms 23032 KB Output is correct
28 Correct 279 ms 24312 KB Output is correct
29 Correct 301 ms 24312 KB Output is correct
30 Correct 152 ms 22520 KB Output is correct
31 Correct 318 ms 24916 KB Output is correct
32 Correct 272 ms 24056 KB Output is correct
33 Correct 333 ms 24764 KB Output is correct
34 Correct 276 ms 24460 KB Output is correct
35 Correct 223 ms 22904 KB Output is correct
36 Correct 72 ms 21368 KB Output is correct
37 Correct 321 ms 25720 KB Output is correct
38 Correct 308 ms 24660 KB Output is correct
39 Correct 313 ms 25080 KB Output is correct