답안 #624151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624151 2022-08-07T10:30:46 Z mdn2002 정렬하기 (IOI15_sorting) C++14
54 / 100
10 ms 852 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int n , m , a [502] , x [20004] , y [20004] , dis [20004] , wrdis [20004] , wra [20004] , P [20004] , Q [20004];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {

    n = N;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        a [i] = S [ i - 1 ];
        a [i] ++;
        wra [ a [i] ] = i;
    }
    m = M;
    for ( int i = 0 ; i < m ; i ++ )
    {
        x [i] = X [i] , y [i] = Y [i];
        x [i] ++ , y [i] ++;
    }
    for ( int i = 1 ; i <= n ; i ++ )
    {
        dis [i] = i;
        wrdis [i] = i;
    }
    for ( int j = m - 1 ; j >= 0 ; j -- )
    {
        swap ( dis [ x [j] ] , dis [ y [j] ] );
        swap ( wrdis [ dis [ x [j] ] ] , wrdis [ dis [ y [j] ] ] );
    }
    set < int > s;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        if ( dis [i] != a [i] )
        {
            s . insert ( dis [i] );
        }
    }

    int r = 0;
    for ( int i = 1 ; i <= m ; i ++ )
    {

        int f = 0;
        for ( int j = 1 ; j <= n ; j ++ )
        {
            if ( a [j] != j ) f = 1;
        }
        if ( f == 0 ) return r;

        swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
        swap ( wra [ a [ x [ i - 1 ] ] ] , wra [ a [ y [ i - 1 ] ] ] );
        swap ( dis [ x [ i - 1 ] ] , dis [ y [ i - 1 ] ] );
        swap ( wrdis [ dis [ x [ i - 1 ] ] ] , wrdis [ dis [ y [ i - 1 ] ] ] );



        P [ i - 1 ] = -1 , Q [ i - 1 ] = -1;
        if ( s . size () )
        {

            int x = * s . begin () , z = wra [x];
            s . erase ( s . begin () );

            swap ( a [ wrdis [x] ] , a [z] );
            swap ( wra [ a [ wrdis [x] ] ] , wra [ a [z] ] );
            if ( a [z] == dis [z] ) s . erase ( s . lower_bound ( dis [z] ) );
            
            P [ i - 1 ] = wrdis [x] - 1 , Q [ i - 1 ] = z - 1;
            r ++;

        }

        if ( P [ i - 1 ] == -1 )
        {
            P [ i - 1 ] = 0;
            Q [ i - 1 ] = 0;
            r ++;
        }

    }
    return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:7:73: warning: declaration of 'Q' shadows a global declaration [-Wshadow]
    7 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                                     ~~~~^~~
sorting.cpp:5:103: note: shadowed declaration is here
    5 | int n , m , a [502] , x [20004] , y [20004] , dis [20004] , wrdis [20004] , wra [20004] , P [20004] , Q [20004];
      |                                                                                                       ^
sorting.cpp:7:64: warning: declaration of 'P' shadows a global declaration [-Wshadow]
    7 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                            ~~~~^~~
sorting.cpp:5:91: note: shadowed declaration is here
    5 | int n , m , a [502] , x [20004] , y [20004] , dis [20004] , wrdis [20004] , wra [20004] , P [20004] , Q [20004];
      |                                                                                           ^
sorting.cpp:63:17: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   63 |             int x = * s . begin () , z = wra [x];
      |                 ^
sorting.cpp:5:23: note: shadowed declaration is here
    5 | int n , m , a [502] , x [20004] , y [20004] , dis [20004] , wrdis [20004] , wra [20004] , P [20004] , Q [20004];
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 216 KB Output is correct
19 Correct 1 ms 216 KB Output is correct
20 Correct 1 ms 216 KB Output is correct
21 Correct 10 ms 836 KB Output is correct
22 Correct 10 ms 756 KB Output is correct
23 Correct 7 ms 852 KB Output is correct
24 Correct 8 ms 852 KB Output is correct
25 Correct 6 ms 852 KB Output is correct
26 Correct 8 ms 840 KB Output is correct
27 Correct 7 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 852 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 852 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -