Submission #985116

# Submission time Handle Problem Language Result Execution time Memory
985116 2024-05-17T10:49:48 Z LucaIlie Sorting (IOI15_sorting) C++17
100 / 100
202 ms 25376 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5;
int t[MAX_N], perm[MAX_N], vis[MAX_N], pos[MAX_N];

vector<pair<int, int>> sol;
int getR( int n, int s[], int k, int x[], int y[], int p[], int q[] ) {
    for ( int i = 0; i < n; i++ )
        t[i] = i, pos[i] = i;

    for ( int i = 0; i < k; i++ ) {
        swap( t[pos[x[i]]], t[pos[y[i]]] );
        swap( pos[x[i]], pos[y[i]] );
    }
    for ( int i = 0; i < n; i++ )
        perm[t[i]] = s[i];//, printf( "%d ", t[i] );
    //printf( "\n\n" );

    for ( int i = 0; i < n; i++ )
        vis[i] = false;

    sol.clear();
    for ( int i = 0; i < n; i++ ) {
        //printf( "%d ", perm[i] );
        if ( vis[i] )
            continue;

        int j = i;
        vector<pair<int, int>> v;
        do {
            vis[j] = true;
            if ( perm[j] != j && j != i )
                v.push_back( { j, perm[j] } );
            j = perm[j];
        } while ( j != i );
        //reverse( v.begin(), v.end() );
        for ( pair<int, int> swp: v )
            sol.push_back( swp );
    }
    //printf( "\n" );

    return sol.size();
}

int findSwapPairs( int n, int s[], int m, int x[], int y[], int p[], int q[] ) {
    int l = -1, r = m;
    while ( r - l > 1 ) {
        int k = (l + r) / 2;

        int rr = getR( n, s, k, x, y, p, q );
        //printf( "%d %d\n", k, rr );
        if ( rr > k )
            l = k;
        else
            r = k;
    }

    getR( n, s, r, x, y, p, q );
    for ( int i = 0; i < n; i++ )
        pos[s[i]] = i;
    for ( int i = 0; i < r; i++ ) {
        //for ( int j = 0; j < n; j++ )
          //  printf( "%d ", pos[j] );
        //printf( "\n" );
        swap( pos[s[x[i]]], pos[s[y[i]]] );
        swap( s[x[i]], s[y[i]] );
        //for ( int j = 0; j < n; j++ )
          //  printf( "%d ", pos[j] );
        //printf( "\n" );
        if ( i < sol.size() ) {
            //printf( "%d %d\n", sol[i].first, sol[i].second );
            p[i] = pos[sol[i].first];
            q[i] = pos[sol[i].second];
            swap( pos[sol[i].first], pos[sol[i].second] );
            swap( s[p[i]], s[q[i]] );
        } else {
            p[i] = 0;
            q[i] = 0;
        }

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

    //for ( int i = 0; i < n; i++ )
  //      cout << s[i] << "\n";

    return r;
}

Compilation message

sorting.cpp: In function 'int getR(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:45:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   45 |     return sol.size();
      |            ~~~~~~~~^~
sorting.cpp:10:56: warning: unused parameter 'p' [-Wunused-parameter]
   10 | int getR( int n, int s[], int k, int x[], int y[], int p[], int q[] ) {
      |                                                    ~~~~^~~
sorting.cpp:10:65: warning: unused parameter 'q' [-Wunused-parameter]
   10 | int getR( int n, int s[], int k, int x[], int y[], int p[], int q[] ) {
      |                                                             ~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         if ( i < sol.size() ) {
      |              ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2500 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2500 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2548 KB Output is correct
11 Correct 1 ms 2500 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2500 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2548 KB Output is correct
11 Correct 1 ms 2500 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 3 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2572 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2500 KB Output is correct
6 Correct 2 ms 2504 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 2 ms 2584 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2584 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2572 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2500 KB Output is correct
6 Correct 2 ms 2504 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 2 ms 2584 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2584 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2504 KB Output is correct
15 Correct 144 ms 21612 KB Output is correct
16 Correct 154 ms 20980 KB Output is correct
17 Correct 181 ms 25376 KB Output is correct
18 Correct 68 ms 19552 KB Output is correct
19 Correct 143 ms 22548 KB Output is correct
20 Correct 161 ms 22236 KB Output is correct
21 Correct 145 ms 22372 KB Output is correct
22 Correct 162 ms 17624 KB Output is correct
23 Correct 166 ms 24420 KB Output is correct
24 Correct 177 ms 24356 KB Output is correct
25 Correct 176 ms 24412 KB Output is correct
26 Correct 145 ms 22496 KB Output is correct
27 Correct 119 ms 19912 KB Output is correct
28 Correct 202 ms 22220 KB Output is correct
29 Correct 168 ms 22072 KB Output is correct
30 Correct 111 ms 19044 KB Output is correct
31 Correct 178 ms 22472 KB Output is correct
32 Correct 158 ms 22412 KB Output is correct
33 Correct 180 ms 24428 KB Output is correct
34 Correct 165 ms 22388 KB Output is correct
35 Correct 139 ms 22456 KB Output is correct
36 Correct 50 ms 17480 KB Output is correct
37 Correct 188 ms 24912 KB Output is correct
38 Correct 182 ms 22336 KB Output is correct
39 Correct 178 ms 24188 KB Output is correct