Submission #496699

# Submission time Handle Problem Language Result Execution time Memory
496699 2021-12-21T23:01:37 Z kevin Sorting (IOI15_sorting) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define ca(v) for(auto i:v) cout<<i<<" ";
#define nl cout<<"\n"

const int MAXN = 5e5 + 5;

int findSwapPairs(int N, vi S, int M, vi X, vi Y, vi P, vi Q){
    int l = 0;
    int r = M-1;
    int ans = M;
    while(l <= r){
        int m = (l+r)/2;
        vi ar = S;
        ca(ar);
        for(int i=0; i<=m; i++){
            swap(ar[X[i]], ar[Y[i]]);
        }
        int cnt = 0;
        vi vis(N, 0);
        for(int i=0; i<N; i++) {
            if(vis[i]) continue;
            cnt++;
            int q = i;
            while(1){
                vis[q] = 1;
                if(vis[ar[q]]) break;
                q = ar[q];
            }
        }
        cnt = N - cnt;
        if(cnt-1 <= m){
            ans = m;
            r = m-1;
        }
        else l = m+1;
    }
    vi vis(N, 0);
    vector<int> ar;
    for(int i=0; i<=ans; i++) swap(ar[X[i]], ar[Y[i]]);
    vi xm, ym;
    int cnt = 0;
    for(int i=0; i<N; i++) {
        if(vis[i]) continue;
        cnt++;
        int q = i;
        while(1){
            vis[q] = 1;
            if(vis[ar[q]]) break;
            xm.push_back(q);
            ym.push_back(ar[q]);
            q = ar[q];
        }
    }
    vector<int> pos(N);
    for(int i=0; i<N; i++) pos[S[i]] = i;
    for(int i=0; i<=ans; i++){
        swap(pos[S[X[i]]], pos[S[Y[i]]]);
        swap(S[X[i]], S[Y[i]]);
        P[i] = pos[S[xm[i]]];
        Q[i] = pos[S[ym[i]]];
    }
    for(int i=1; i<=N - cnt; i++){
        P[i+ans] = 0;
        S[i+ans] = 0;
    }
    return ans+1;
}

// int main()
// {
//     // ios_base::sync_with_stdio(0); cin.tie(0);
//     if (fopen("input.in", "r")) freopen("input.in", "r", stdin);
//     int N, M; cin>>N>>M;
//     vi S(N), X(M), Y(M), P, Q;
//     for(int i=0; i<N; i++) cin>>S[i];
//     for(int i=0; i<M; i++) cin>>X[i]>>Y[i];
//     cout<<findSwapPairs(N, S, M, X, Y, P, Q);
// }

Compilation message

/usr/bin/ld: /tmp/cckCzaeR.o: in function `main':
grader.c:(.text.startup+0x4eb): undefined reference to `findSwapPairs(int, int*, int, int*, int*, int*, int*)'
collect2: error: ld returned 1 exit status