Submission #795707

# Submission time Handle Problem Language Result Execution time Memory
795707 2023-07-27T13:10:14 Z jasmin Sorting (IOI15_sorting) C++17
100 / 100
128 ms 19624 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int cnt_cycle(vector<int>& s, int n){

    vector<bool> vis(n, false);
    int cnt=0;
    for(int v=0; v<n; v++){
        if(vis[v]) continue;
        
        cnt++;

        int mom=s[v];
        while(!vis[mom]){

            vis[mom]=true;
            mom=s[mom];
        }
    }

    return cnt;
}

int cnt_cycles(int rounds, vector<int> s, int x[], int y[], int n){
    for(int i=0; i<rounds; i++){
        swap(s[x[i]], s[y[i]]);
    }

    int cnt=0;
    vector<bool> vis(n);
    for(int v=0; v<n; v++){
        if(!vis[v]) cnt++;

        while(!vis[v]){
            vis[v]=true;

            v=s[v];
        }
    }
    return cnt;
}

int findSwapPairs(int n, int S[], int m, int x[], int y[], int p[], int q[]) {
    
    //initialize
    vector<int> s(n);
    for(int i=0; i<n; i++){
        s[i] = S[i];
    }

    //cnt minimal rounds
    /*int rounds=-1;
    int cnt = n-cnt_cycle(s, n);
    for(int i=0; i<m; i++){

        if(cnt<=i){
            rounds = i;
            break;
        }

        swap(s[x[i]], s[y[i]]);
        cnt = n-cnt_cycle(s, n);
    }

    if(rounds==-1 && cnt<=m){
        rounds=m;
    }
    assert(rounds!=-1);*/

    int l=0; int r=m;
    int ans=m;
    while(l<=r){
        int mi=l+(r-l)/2;

        if(n-cnt_cycles(mi, s, x, y, n) <= mi){
            ans=mi;
            r=mi-1;
        }
        else{
            l=mi+1;
        }
    }
    int rounds=ans;

    for(int i=0; i<rounds; i++){
        swap(s[x[i]], s[y[i]]);
    }

    //find swaps
    deque<int> missing;
    vector<int> next(n);

    vector<bool> vis(n, false);
    for(int v=0; v<n; v++){
        if(vis[v]) continue;

        int mom = v;
        while(!vis[mom]){
            vis[mom]=true;

            missing.push_back(s[mom]);
            next[s[mom]] = s[s[mom]];

            mom=s[mom];
        }
        missing.pop_back(); //last in cycle doesn't need to be swapped anymore
    }

    vector<int> pos(n);
    for(int i=0; i<n; i++){
        pos[s[i]]=i;
    }

    for(int i=rounds-1; i>=0; i--){

        if(missing.empty()){

            p[i]=q[i]=0;
            continue;
        }

        int a=missing.front(); missing.pop_front();
        int b=next[a];

        //cout << a << " " << b << ": " << pos[a] << " " << pos[b] << "\n";

        p[i] = pos[a];
        q[i] = pos[b];

        swap(pos[s[x[i]]], pos[s[y[i]]]);
        swap(s[x[i]], s[y[i]]);
    }

    return rounds;
}


# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 444 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 440 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 440 KB Output is correct
9 Correct 1 ms 436 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 1 ms 380 KB Output is correct
12 Correct 1 ms 372 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 440 KB Output is correct
9 Correct 1 ms 436 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 1 ms 380 KB Output is correct
12 Correct 1 ms 372 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 103 ms 17168 KB Output is correct
16 Correct 105 ms 17604 KB Output is correct
17 Correct 123 ms 18696 KB Output is correct
18 Correct 60 ms 15500 KB Output is correct
19 Correct 103 ms 17948 KB Output is correct
20 Correct 113 ms 18672 KB Output is correct
21 Correct 109 ms 18660 KB Output is correct
22 Correct 93 ms 13940 KB Output is correct
23 Correct 128 ms 19368 KB Output is correct
24 Correct 118 ms 19112 KB Output is correct
25 Correct 128 ms 18832 KB Output is correct
26 Correct 107 ms 18616 KB Output is correct
27 Correct 100 ms 17916 KB Output is correct
28 Correct 115 ms 18860 KB Output is correct
29 Correct 124 ms 18368 KB Output is correct
30 Correct 81 ms 17320 KB Output is correct
31 Correct 117 ms 18728 KB Output is correct
32 Correct 106 ms 18696 KB Output is correct
33 Correct 115 ms 19004 KB Output is correct
34 Correct 113 ms 18964 KB Output is correct
35 Correct 101 ms 17832 KB Output is correct
36 Correct 50 ms 16580 KB Output is correct
37 Correct 119 ms 19624 KB Output is correct
38 Correct 113 ms 18876 KB Output is correct
39 Correct 113 ms 18896 KB Output is correct