Submission #46937

# Submission time Handle Problem Language Result Execution time Memory
46937 2018-04-25T08:26:43 Z mAng0 Sorting (IOI15_sorting) C++17
100 / 100
214 ms 13020 KB
#include "sorting.h"

int S2[200001], vis[200001];
int perm[200001], inv[200001];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
//int findSwapPairs(){
    int ss = 0, ee = M, R = 0, now = 0;
    while(ss <= ee){
        int mid = (ss+ee) / 2;
        for(int i=0;i<N;i++){
            S2[i] = S[i];
            vis[i] = 0;
        }
        for(int i=now;i<mid;i++){
            int tmp = S2[X[i]]; S2[X[i]] = S2[Y[i]]; S2[Y[i]] = tmp;
        }
        int cnt = 0;
        for(int i=0;i<N;i++){
            if(vis[i]) continue;
            int ii = S2[i];
            while(ii != i){
                vis[ii] = 1;
                cnt++;
                ii = S2[ii];
            }
        }
        if(cnt <= mid){
            R = mid;
            ee = mid - 1;
        }else{
            now = mid;
            for(int i=0;i<N;i++) S[i] = S2[i];
            ss = mid + 1;
        }
    }
    for(int i=now;i<R;i++){
        int tmp = S[X[i]]; S[X[i]] = S[Y[i]]; S[Y[i]] = tmp;
    }
    for(int i=0;i<N;i++){
        vis[i] = 0;
        perm[i] = i;
        inv[i] = i;
    }
    int cnt = 0;
    for(int i=0;i<N;i++){
        if(vis[i]) continue;
        while(S[i] != i){
            vis[S[i]] = 1;
            P[cnt] = i; Q[cnt] = S[i];
            int nxt = S[i];
            S[i] = S[nxt];
            S[nxt] = nxt;
            cnt++;
        }
    }
    for(int i=R-2;i>=0;i--){
        int tmp;
        int p1 = X[i+1], q1 = Y[i+1];
        tmp = perm[p1]; perm[p1] = perm[q1]; perm[q1] = tmp;
        int p2 = perm[p1], q2 = perm[q1];
        tmp = inv[p2]; inv[p2] = inv[q2]; inv[q2] = tmp;
        P[i] = inv[P[i]]; Q[i] = inv[Q[i]];
    }
    return R;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 3 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 3 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 2 ms 432 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 3 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 2 ms 432 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 176 ms 10840 KB Output is correct
16 Correct 177 ms 11124 KB Output is correct
17 Correct 212 ms 12152 KB Output is correct
18 Correct 71 ms 8312 KB Output is correct
19 Correct 143 ms 10616 KB Output is correct
20 Correct 143 ms 11000 KB Output is correct
21 Correct 137 ms 11016 KB Output is correct
22 Correct 173 ms 11768 KB Output is correct
23 Correct 197 ms 11960 KB Output is correct
24 Correct 167 ms 12152 KB Output is correct
25 Correct 194 ms 12412 KB Output is correct
26 Correct 133 ms 10872 KB Output is correct
27 Correct 139 ms 10556 KB Output is correct
28 Correct 192 ms 11768 KB Output is correct
29 Correct 197 ms 12024 KB Output is correct
30 Correct 96 ms 10012 KB Output is correct
31 Correct 183 ms 12268 KB Output is correct
32 Correct 182 ms 11588 KB Output is correct
33 Correct 214 ms 12380 KB Output is correct
34 Correct 190 ms 12024 KB Output is correct
35 Correct 137 ms 10616 KB Output is correct
36 Correct 53 ms 8824 KB Output is correct
37 Correct 204 ms 13020 KB Output is correct
38 Correct 210 ms 12336 KB Output is correct
39 Correct 175 ms 12728 KB Output is correct