Submission #134467

# Submission time Handle Problem Language Result Execution time Memory
134467 2019-07-22T18:09:24 Z evpipis Sorting (IOI15_sorting) C++11
100 / 100
285 ms 26776 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;

const int len = 6e5+5;
int fin[len], arr[len], vis[len], plate[len], pos[len];
int n, m;
ii mov[len];

bool check(int x){
    for (int i = 0; i < n; i++)
        fin[i] = i;
    for (int i = 0; i < x; i++)
        swap(fin[mov[i].fi], fin[mov[i].se]);

    for (int i = 0; i < n; i++)
        plate[i] = fin[arr[i]], vis[i] = 0;

    int rem = n;
    for (int i = 0; i < n; i++){
        if (vis[i]) continue;

        int cur = plate[i];
        vis[i] = 1;
        while (cur != i)
            vis[cur] = 1, cur = plate[cur];

        rem--;
    }

    return (rem <= x);
}

int bs(){
    int l = 0, r = m, ans;
    while (l <= r){
        int mid = (l+r)/2;
        if (check(mid))
            ans = mid, r = mid-1;
        else
            l = mid+1;
    }

    return ans;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    //freopen("CON", "w", stdin);
    //printf("hello\n");
    n = N, m = M;
    for (int i = 0; i < n; i++)
        arr[i] = S[i];
    for (int i = 0; i < m; i++)
        mov[i] = mp(X[i], Y[i]);

    int k = bs();
    check(k);

    //printf("k = %d\n", k);

    for (int i = 0; i < n; i++)
        fin[i] = i, pos[i] = i;

    /*for (int i = 0; i < n; i++)
        printf("%d ", pos[i]);
    printf("\n");*/

    for (int i = 0, j = 0; i < k; i++){
        //printf("i = %d\n", i);
        int a = mov[i].fi, b = mov[i].se;
        //printf("a = %d, b = %d\n", a, b);
        swap(pos[fin[a]], pos[fin[b]]);
        swap(fin[a], fin[b]);

        /*for (int x = 0; x < n; x++)
            printf("%d ", pos[x]);
        printf("\n");*/

        while (j < n && plate[j] == j)
            j++;

        if (j < n){
            a = j, b = plate[j];
            P[i] = pos[a];
            Q[i] = pos[b];
            plate[a] = plate[b];
            plate[b] = b;
        }
        else{
            P[i] = 0;
        Q[i] = 0;
        }
    }

    /*for (int i = 0; i < k; i++){
        int a = mov[i].fi, b = mov[i].se;
        swap(arr[a], arr[b]);

        a = P[i], b = Q[i];
        swap(arr[a], arr[b]);

        for (int j = 0; j < n; j++)
            printf("%d ", arr[j]);
        printf("\n");
    }*/

    return k;
}

Compilation message

sorting.cpp: In function 'int bs()':
sorting.cpp:50:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
     return ans;
            ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 760 KB Output is correct
22 Correct 3 ms 760 KB Output is correct
23 Correct 3 ms 760 KB Output is correct
24 Correct 3 ms 760 KB Output is correct
25 Correct 3 ms 760 KB Output is correct
26 Correct 3 ms 760 KB Output is correct
27 Correct 3 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 3 ms 636 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 3 ms 636 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 190 ms 23416 KB Output is correct
16 Correct 198 ms 23928 KB Output is correct
17 Correct 242 ms 25364 KB Output is correct
18 Correct 80 ms 21340 KB Output is correct
19 Correct 173 ms 24584 KB Output is correct
20 Correct 188 ms 25248 KB Output is correct
21 Correct 175 ms 25336 KB Output is correct
22 Correct 192 ms 20600 KB Output is correct
23 Correct 225 ms 26232 KB Output is correct
24 Correct 242 ms 26000 KB Output is correct
25 Correct 241 ms 25956 KB Output is correct
26 Correct 183 ms 25264 KB Output is correct
27 Correct 175 ms 24408 KB Output is correct
28 Correct 232 ms 25724 KB Output is correct
29 Correct 285 ms 25676 KB Output is correct
30 Correct 125 ms 24100 KB Output is correct
31 Correct 228 ms 26316 KB Output is correct
32 Correct 217 ms 25208 KB Output is correct
33 Correct 238 ms 26016 KB Output is correct
34 Correct 222 ms 25976 KB Output is correct
35 Correct 170 ms 24184 KB Output is correct
36 Correct 65 ms 23032 KB Output is correct
37 Correct 240 ms 26776 KB Output is correct
38 Correct 220 ms 26232 KB Output is correct
39 Correct 231 ms 25720 KB Output is correct