Submission #105053

# Submission time Handle Problem Language Result Execution time Memory
105053 2019-04-10T10:20:44 Z Alexa2001 Sorting (IOI15_sorting) C++17
100 / 100
722 ms 29380 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 5;

bool used[Nmax];
int p[Nmax], P[Nmax], A[Nmax], wA[Nmax], B[Nmax], wB[Nmax], wp[Nmax];


vector< pair<int,int> > get_moves(int n, int p[])
{
    int i;
    for(i=0; i<n; ++i) used[i] = 0;
    vector< pair<int,int> > mv;

    for(i=0; i<n; ++i)
        if(!used[i])
        {
            int x = i;
            vector<int> cycle;

            do
            {
                used[x] = 1;
                cycle.push_back(x);
                x = p[x];
            }
            while(!used[x]);

            for(int j = 0; j + 1 < cycle.size(); ++j)
                mv.push_back({p[cycle[j]], p[cycle[j+1]]});
        }
    return mv;
}

bool check(int n, int m, int S[], int x[], int y[], int op1[], int op2[])
{
    int i;
    for(i=0; i<n; ++i) p[i] = i, wp[i] = i;
    for(i=0; i<m; ++i)
    {
        int id1, id2;
        id1 = wp[x[i]]; id2 = wp[y[i]];
        swap(wp[p[id1]], wp[p[id2]]);
        swap(p[id1], p[id2]);
    }

    for(i=0; i<n; ++i) P[p[i]] = S[i];

    auto o = get_moves(n, P);

    if(o.size() > m) return 0;
    o.resize(m);

    for(i=0; i<n; ++i) A[i] = wA[i] = i, B[i] = S[i], wB[S[i]] = i;

    for(i=0; i<m; ++i)
    {
        int id1, id2;

        id1 = wA[x[i]]; id2 = wA[y[i]];

        swap(wA[A[id1]], wA[A[id2]]);
        swap(A[id1], A[id2]);

        tie(id1, id2) = o[i];
        id1 = wB[id1]; id2 = wB[id2];

        op1[i] = A[id1];
        op2[i] = A[id2];

        swap(wB[B[id1]], wB[B[id2]]);
        swap(B[id1], B[id2]);
    }
    return 1;
}


int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
    int st, dr, mid;

    st = 0; dr = M;
    while(st <= dr)
    {
        mid = (st + dr) / 2;
        if(check(N, mid, S, X, Y, P, Q)) dr = mid - 1;
            else st = mid + 1;
    }

    check(N, st, S, X, Y, P, Q);

    return st;
}

Compilation message

sorting.cpp: In function 'std::vector<std::pair<int, int> > get_moves(int, int*)':
sorting.cpp:12:49: warning: declaration of 'p' shadows a global declaration [-Wshadow]
 vector< pair<int,int> > get_moves(int n, int p[])
                                                 ^
sorting.cpp:9:5: note: shadowed declaration is here
 int p[Nmax], P[Nmax], A[Nmax], wA[Nmax], B[Nmax], wB[Nmax], wp[Nmax];
     ^
sorting.cpp:32:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j + 1 < cycle.size(); ++j)
                            ~~~~~~^~~~~~~~~~~~~~
sorting.cpp: In function 'bool check(int, int, int*, int*, int*, int*, int*)':
sorting.cpp:54:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(o.size() > m) return 0;
        ~~~~~~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:81:76: warning: declaration of 'P' shadows a global declaration [-Wshadow]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
                                                                            ^
sorting.cpp:9:14: note: shadowed declaration is here
 int p[Nmax], P[Nmax], A[Nmax], wA[Nmax], B[Nmax], wB[Nmax], wp[Nmax];
              ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 380 KB Output is correct
5 Correct 4 ms 544 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 380 KB Output is correct
17 Correct 4 ms 544 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 3 ms 768 KB Output is correct
23 Correct 4 ms 768 KB Output is correct
24 Correct 3 ms 768 KB Output is correct
25 Correct 4 ms 768 KB Output is correct
26 Correct 5 ms 768 KB Output is correct
27 Correct 4 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 4 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 4 ms 640 KB Output is correct
12 Correct 4 ms 640 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 4 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 4 ms 640 KB Output is correct
12 Correct 4 ms 640 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 426 ms 26036 KB Output is correct
16 Correct 538 ms 26452 KB Output is correct
17 Correct 550 ms 28368 KB Output is correct
18 Correct 308 ms 24580 KB Output is correct
19 Correct 444 ms 27236 KB Output is correct
20 Correct 483 ms 27792 KB Output is correct
21 Correct 444 ms 27612 KB Output is correct
22 Correct 456 ms 22836 KB Output is correct
23 Correct 543 ms 29380 KB Output is correct
24 Correct 679 ms 29376 KB Output is correct
25 Correct 698 ms 28808 KB Output is correct
26 Correct 641 ms 26060 KB Output is correct
27 Correct 566 ms 25300 KB Output is correct
28 Correct 645 ms 28108 KB Output is correct
29 Correct 668 ms 27692 KB Output is correct
30 Correct 615 ms 24820 KB Output is correct
31 Correct 722 ms 28380 KB Output is correct
32 Correct 427 ms 28320 KB Output is correct
33 Correct 613 ms 29140 KB Output is correct
34 Correct 513 ms 27660 KB Output is correct
35 Correct 451 ms 26748 KB Output is correct
36 Correct 258 ms 24696 KB Output is correct
37 Correct 545 ms 27400 KB Output is correct
38 Correct 548 ms 26488 KB Output is correct
39 Correct 503 ms 26688 KB Output is correct