Submission #364875

# Submission time Handle Problem Language Result Execution time Memory
364875 2021-02-10T10:37:30 Z 79brue Sorting (IOI15_sorting) C++14
0 / 100
4 ms 876 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

typedef long long ll;

int n, q;
int arr[200002];
int a[600002], b[600002];

int lastOccurence[200002];
int tarr[200002];
int idx[200002];
stack<int> stk;

bool able(int x, bool print, int P[], int Q[]){
    for(int i=0; i<n; i++){
        tarr[i] = arr[i];
        idx[tarr[i]] = i;
        lastOccurence[i] = 0;
    }
    for(int i=1; i<=x; i++){
        lastOccurence[a[i]] = i;
        lastOccurence[b[i]] = i;
    }
    while(!stk.empty()) stk.pop();
    for(int i=0; i<n; i++){
        if(lastOccurence[i] == 0) stk.push(i);
    }

    for(int i=1; i<=x; i++){
        int u = a[i], v = b[i];
        swap(tarr[u], tarr[v]);
        idx[tarr[u]] = u;
        idx[tarr[v]] = v;

        if(lastOccurence[u] == i) stk.push(u);
        if(lastOccurence[v] == i) stk.push(v);

        while(!stk.empty() && tarr[stk.top()] == stk.top()) stk.pop();
        if(stk.empty()){
            if(print) P[i] = Q[i] = 0;
            continue;
        }
        u = stk.top(); stk.pop();
        v = idx[u];
        if(print) P[i] = u, Q[i] = v;
        swap(tarr[u], tarr[v]);
        idx[tarr[u]] = u;
        idx[tarr[v]] = v;
    }

    for(int i=0; i<n; i++){
        if(tarr[i] != i) return false;
    }
    return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
    n = N;
    for(int i=0; i<n; i++){
        arr[i] = S[i];
    }
    q = M;
    for(int i=1; i<=q; i++){
        a[i] = X[i-1], b[i] = Y[i-1];
    }

    int MIN = 0, MAX = q, ANS = q+1;
    while(MIN <= MAX){
        int MID = (MIN + MAX) / 2;
        if(able(MID, 0, P, Q)){
            ANS = MID;
            MAX = MID-1;
        }
        else MIN = MID+1;
    }

    assert(ANS <= q);

    able(ANS, 1, P, Q);
  	return ANS;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 876 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 876 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -