Submission #257144

# Submission time Handle Problem Language Result Execution time Memory
257144 2020-08-03T16:18:58 Z eohomegrownapps Sorting (IOI15_sorting) C++14
100 / 100
558 ms 83448 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

struct SwapList{
    vector<int> index;
    vector<int> arr;
    int n;
    SwapList(int _n){
        n=_n;
        index.resize(n);
        arr.resize(n);
    }

    void set(int ind, int val){
        arr[ind]=val;
        index[val]=ind;
    }

    int get(int ind){
        return arr[ind];
    }

    int indexOf(int ind){
        return index[ind];
    }

    void swapIndices(int a, int b){
        swap(arr[a],arr[b]);
        swap(index[arr[a]],index[arr[b]]);
    }

    void swapValues(int a, int b){
        swapIndices(indexOf(a), indexOf(b));
    }
};

int n;

int *x, *y, *s, *p, *q;

bool success(int m){
    SwapList *nums = new SwapList(n);
    SwapList *current = new SwapList(n);
    for (int i = 0; i<n; i++){
        nums->set(i,i);
        current->set(i,s[i]);
    }
    for (int i = m-1; i>=0; i--){
        nums->swapIndices(x[i],y[i]);
    }

    int curel = 0;

    for (int i = 0; i<m; i++){
        //perform opponent's swap
        current->swapIndices(x[i],y[i]);
        nums->swapIndices(x[i],y[i]);
        //which to move?
        while (curel<n&&current->indexOf(curel)==nums->indexOf(curel)){
            curel++;
        }
        if (curel==n){
            for (int j = i; j<m; j++){
                p[i]=0;
                q[i]=0;
            }
            return true;
        }
        //indices to swap:
        int curel_pos_current = current->indexOf(curel);
        int curel_pos_nums = nums->indexOf(curel);
        p[i]=curel_pos_current;
        q[i]=curel_pos_nums;
        current->swapIndices(curel_pos_current,curel_pos_nums);
        /*for (int i = 0; i<n; i++){
            cout<<current->get(i)<<' ';
        }cout<<'\n';*/
    }
    for (int i = 0; i<n; i++){
        if (current->get(i)!=i){return false;}
    }
    return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n=N;x=X;y=Y;s=S;p=P;q=Q;
    int l = 0, r = M;
    while (l<=r){
        int mid = (l+r)/2;
        bool s = success(mid);
        //cout<<mid<<' '<<s<<'\n';
        if (s){
            r=mid-1;
        } else {
            l=mid+1;
        }
    }
    //l is correct value
    success(l);
	return l;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:91:14: warning: declaration of 's' shadows a global declaration [-Wshadow]
         bool s = success(mid);
              ^
sorting.cpp:40:14: note: shadowed declaration is here
 int *x, *y, *s, *p, *q;
              ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 2 ms 640 KB Output is correct
22 Correct 2 ms 664 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 896 KB Output is correct
2 Correct 4 ms 896 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 2 ms 896 KB Output is correct
8 Correct 3 ms 896 KB Output is correct
9 Correct 3 ms 896 KB Output is correct
10 Correct 3 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 1 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 896 KB Output is correct
2 Correct 4 ms 896 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 2 ms 896 KB Output is correct
8 Correct 3 ms 896 KB Output is correct
9 Correct 3 ms 896 KB Output is correct
10 Correct 3 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 1 ms 896 KB Output is correct
15 Correct 329 ms 71968 KB Output is correct
16 Correct 400 ms 73720 KB Output is correct
17 Correct 550 ms 80504 KB Output is correct
18 Correct 140 ms 72568 KB Output is correct
19 Correct 324 ms 78200 KB Output is correct
20 Correct 437 ms 83448 KB Output is correct
21 Correct 362 ms 80252 KB Output is correct
22 Correct 323 ms 76792 KB Output is correct
23 Correct 335 ms 80632 KB Output is correct
24 Correct 525 ms 79224 KB Output is correct
25 Correct 539 ms 78456 KB Output is correct
26 Correct 362 ms 82808 KB Output is correct
27 Correct 294 ms 77688 KB Output is correct
28 Correct 495 ms 79324 KB Output is correct
29 Correct 502 ms 78200 KB Output is correct
30 Correct 219 ms 77304 KB Output is correct
31 Correct 505 ms 79864 KB Output is correct
32 Correct 352 ms 77908 KB Output is correct
33 Correct 558 ms 79328 KB Output is correct
34 Correct 412 ms 78968 KB Output is correct
35 Correct 341 ms 77048 KB Output is correct
36 Correct 111 ms 76664 KB Output is correct
37 Correct 544 ms 81656 KB Output is correct
38 Correct 527 ms 78456 KB Output is correct
39 Correct 520 ms 78712 KB Output is correct