Submission #207271

# Submission time Handle Problem Language Result Execution time Memory
207271 2020-03-06T21:53:23 Z oscarsierra12 Sorting (IOI15_sorting) C++14
100 / 100
603 ms 27156 KB
#include <bits/stdc++.h>

#define ff      first
#define ss      second
#define pb      push_back

using namespace std ;

typedef long long      ll ;
typedef pair<int,int>  ii ;

const int n = 600010 ;

int prv[n], nxt[n] ;
int ind[n], ele[n] ;
int rind[n] ;
int P[n], Q[n] ;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int p[], int q[]) {
    for ( int i = 0 ; i < N ; ++i )
        rind[S[i]] = i ;
    int lw = 0, hg = M ;
    vector <int> ans ;
    int lst = 0;
    while ( lw < hg ) {
        int i = (lw+hg)/2 ;
        for ( int j = 0 ; j < N ; ++j ) prv[j] = nxt[j] = j ;
        for ( int j = 0 ; j < i ; ++j ) {
            int A = prv[X[j]] ;
            int B = prv[Y[j]] ;
            nxt[A] = Y[j] ;
            prv[Y[j]] = A ;
            nxt[B] = X[j] ;
            prv[X[j]] = B ;
        }
        for ( int j = 0 ; j < N ; ++j ) {
            ele[j] = S[prv[j]] ;
            ind[S[prv[j]]] = j ;
        }
        vector <ii> swaps ;
        for ( int j = 0 ; j < N ; ++j ) {
            if ( ele[j] == j ) continue ;
            swaps.pb ({rind[ele[j]], rind[ele[ind[j]]]}) ;
            ind[ele[j]] = ind[j] ;
            swap (ele[j], ele[ind[j]]) ;
        }
        for ( int j = 0 ; j < N ; ++j ) prv[j] = nxt[j] = j ;
        int id = 0 ;
        for ( int j = 0 ; j < i ; ++j ) {
            int A = prv[X[j]] ;
            int B = prv[Y[j]] ;
            nxt[A] = Y[j] ;
            prv[Y[j]] = A ;
            nxt[B] = X[j] ;
            prv[X[j]] = B ;
            if ( id < swaps.size() ) {
                P[j] = nxt[swaps[id].ff] ;
                Q[j] = nxt[swaps[id].ss] ;
            }
            else {
                P[j] = 0 ;
                Q[j] = 0 ;
            }
            A = prv[P[j]] ;
            B = prv[Q[j]] ;
            nxt[A] = Q[j] ;
            prv[Q[j]] = A ;
            nxt[B] = P[j] ;
            prv[P[j]] = B ;
            id++ ;
        }
        if ( swaps.size() <= i ) {
            hg = i ;
            lst = i ;
            for ( int j = 0 ; j < i ; ++j ) p[j] = P[j], q[j] = Q[j] ;
        }
        else lw = i+1 ;
    }
    return lst ;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:56:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if ( id < swaps.size() ) {
                  ~~~^~~~~~~~~~~~~~
sorting.cpp:72:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if ( swaps.size() <= i ) {
              ~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 380 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 380 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 504 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 6 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 6 ms 632 KB Output is correct
22 Correct 6 ms 632 KB Output is correct
23 Correct 6 ms 632 KB Output is correct
24 Correct 6 ms 632 KB Output is correct
25 Correct 6 ms 632 KB Output is correct
26 Correct 6 ms 632 KB Output is correct
27 Correct 6 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 504 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 7 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 6 ms 508 KB Output is correct
6 Correct 6 ms 504 KB Output is correct
7 Correct 7 ms 504 KB Output is correct
8 Correct 7 ms 508 KB Output is correct
9 Correct 7 ms 504 KB Output is correct
10 Correct 7 ms 632 KB Output is correct
11 Correct 7 ms 504 KB Output is correct
12 Correct 7 ms 504 KB Output is correct
13 Correct 7 ms 504 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 504 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 7 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 6 ms 508 KB Output is correct
6 Correct 6 ms 504 KB Output is correct
7 Correct 7 ms 504 KB Output is correct
8 Correct 7 ms 508 KB Output is correct
9 Correct 7 ms 504 KB Output is correct
10 Correct 7 ms 632 KB Output is correct
11 Correct 7 ms 504 KB Output is correct
12 Correct 7 ms 504 KB Output is correct
13 Correct 7 ms 504 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 381 ms 16336 KB Output is correct
16 Correct 440 ms 24516 KB Output is correct
17 Correct 582 ms 25824 KB Output is correct
18 Correct 151 ms 24464 KB Output is correct
19 Correct 371 ms 25940 KB Output is correct
20 Correct 374 ms 26552 KB Output is correct
21 Correct 381 ms 26548 KB Output is correct
22 Correct 382 ms 21096 KB Output is correct
23 Correct 436 ms 26640 KB Output is correct
24 Correct 603 ms 26444 KB Output is correct
25 Correct 596 ms 26060 KB Output is correct
26 Correct 391 ms 25308 KB Output is correct
27 Correct 349 ms 24704 KB Output is correct
28 Correct 525 ms 26256 KB Output is correct
29 Correct 520 ms 26072 KB Output is correct
30 Correct 268 ms 24088 KB Output is correct
31 Correct 540 ms 26512 KB Output is correct
32 Correct 413 ms 25860 KB Output is correct
33 Correct 602 ms 26312 KB Output is correct
34 Correct 485 ms 26180 KB Output is correct
35 Correct 387 ms 25428 KB Output is correct
36 Correct 121 ms 23032 KB Output is correct
37 Correct 592 ms 27156 KB Output is correct
38 Correct 565 ms 26096 KB Output is correct
39 Correct 575 ms 26188 KB Output is correct