Submission #90237

# Submission time Handle Problem Language Result Execution time Memory
90237 2018-12-21T01:32:11 Z Retro3014 Sorting (IOI15_sorting) C++17
100 / 100
246 ms 18168 KB
#include "sorting.h"
#include <vector>

using namespace std;
#define MAX_N 200000

vector<int> x, y, arr, idx;
vector<int> v;
bool vst[MAX_N+1];
int n, m;
int R;

int dfs(int x){
    if(vst[x])  return 0;
    vst[x]=true;
    return dfs(v[x])+1;
}

bool chk(){
    int cnt=0;
    v.clear();
    for(int i=0; i<n; i++){
        v.push_back(arr[i]);
        vst[i]=false;
    }
    for(int i=0; i<R; i++){
        int tmp=v[x[i]];
        v[x[i]]=v[y[i]];
        v[y[i]]=tmp;
    }
    for(int i=0; i<n; i++){
        if(!vst[i] && v[i]!=i){
            vst[i]=true;
            cnt+=dfs(v[i]);
        }
    }
    return (cnt<=R);
}

void sw(int aa, int bb){
    int tmp=idx[v[aa]];
    idx[v[aa]]=idx[v[bb]];
    idx[v[bb]]=tmp;
    tmp=v[aa];
    v[aa]=v[bb];
    v[bb]=tmp;
}


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<N; i++){
        arr.push_back(S[i]);
        idx.push_back(0);
    }
    for(int i=0; i<M; i++){
        x.push_back(X[i]);
        y.push_back(Y[i]);
    }
    int s=0, e=M-1;
    while(s<e){
        R=(s+e)/2;
        if(chk()){
            e=R;
        }else{
            s=R+1;
        }
    }
    R=(s+e)/2;
    v.clear();
    int cnt=0;
    for(int i=0; i<n; i++){
        v.push_back(arr[i]);
        vst[i]=false;
    }
    for(int i=0; i<R; i++){
        int tmp=v[x[i]];
        v[x[i]]=v[y[i]];
        v[y[i]]=tmp;
    }
    for(int i=0; i<n; i++){
        while(v[i]!=i){
            P[cnt]=v[i];
            Q[cnt]=v[v[i]];
            cnt++;
            int tmp=v[i];
            v[i]=v[v[i]];
            v[tmp]=tmp;
        }
    }
    v.clear();
    for(int i=0; i<n; i++){
        v.push_back(arr[i]);
        idx[v[i]]=i;
    }
    while(cnt<R){
        P[cnt]=1;
        Q[cnt]=1;
        cnt++;
    }
    for(int i=0; i<R; i++){
        sw(X[i], Y[i]);
        P[i]=idx[P[i]];
        Q[i]=idx[Q[i]];
        sw(P[i], Q[i]);
    }
	return R;
}


Compilation message

sorting.cpp: In function 'int dfs(int)':
sorting.cpp:13:14: warning: declaration of 'x' shadows a global declaration [-Wshadow]
 int dfs(int x){
              ^
sorting.cpp:7:13: note: shadowed declaration is here
 vector<int> x, y, arr, idx;
             ^
# 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 1 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 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 1 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 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 1 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 188 ms 15528 KB Output is correct
16 Correct 208 ms 15992 KB Output is correct
17 Correct 239 ms 17136 KB Output is correct
18 Correct 94 ms 13164 KB Output is correct
19 Correct 184 ms 15608 KB Output is correct
20 Correct 175 ms 16108 KB Output is correct
21 Correct 182 ms 16248 KB Output is correct
22 Correct 192 ms 16888 KB Output is correct
23 Correct 211 ms 17144 KB Output is correct
24 Correct 213 ms 17216 KB Output is correct
25 Correct 238 ms 17420 KB Output is correct
26 Correct 187 ms 16260 KB Output is correct
27 Correct 166 ms 15484 KB Output is correct
28 Correct 223 ms 16892 KB Output is correct
29 Correct 219 ms 17020 KB Output is correct
30 Correct 122 ms 14968 KB Output is correct
31 Correct 214 ms 17400 KB Output is correct
32 Correct 216 ms 16636 KB Output is correct
33 Correct 242 ms 17356 KB Output is correct
34 Correct 213 ms 17016 KB Output is correct
35 Correct 184 ms 15608 KB Output is correct
36 Correct 68 ms 13952 KB Output is correct
37 Correct 214 ms 18168 KB Output is correct
38 Correct 246 ms 17248 KB Output is correct
39 Correct 238 ms 17680 KB Output is correct