답안 #621096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
621096 2022-08-03T12:14:27 Z mdn2002 정렬하기 (IOI15_sorting) C++14
20 / 100
4 ms 468 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005];

bool ck ( int xx )
{
    for ( int i = 1 ; i <= n ; i ++ )
    {
        a [i] = aa [i];
        wra [ a [i] ] = i;
    }
    int mm = xx;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        dis [i] = i;
        wrdis [ dis [i] ] = i;
    }
    for ( int j = mm - 1 ; j >= 0 ; j -- )
    {
        swap ( dis [ x [j] ] , dis [ y [j] ] );
        swap ( wrdis [ x [j] ] , wrdis [ y [j] ] );
    }
    set < int > dq;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        if ( dis [i] != a [i] ) dq . insert ( i );
    }
    for ( int i = 1 ; i <= mm ; i ++ )
    {
        if ( dq . size () == 0 ) return 1;
        swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
        swap ( dis [ x [ i - 1 ] ] , dis [ y [ i - 1 ] ] );
        swap ( wrdis [ x [ i - 1 ] ] , wrdis [ y [ i - 1 ] ] );
        swap ( wra [ a [ x [ i - 1 ] ] ] , wra [ a [ y [ i - 1 ] ] ] );


        int x = * dq . begin ();
        dq . erase ( dq . begin () );

        int y = wra [ dis [x] ];

        swap ( a [x] , a [y] );
        swap ( wra [ a [x] ] , wra [ a [y] ] );

        if ( dis [y] == a [y] ) dq . erase ( dq . lower_bound ( y ) );
    }
    if ( dq . size () == 0 ) return 1;
    return 0;
}

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] ++;
        aa [i] = a [i];
    }
    m = M;
    for ( int i = 0 ; i < m ; i ++ )
    {
        x [i] = X [i] , y [i] = Y [i];
        x [i] ++ , y [i] ++;
    }
    int l = 0 , r = m + 1 , mid;
    while ( l < r )
    {
        mid = ( l + r ) / 2;
        if ( ck ( mid ) ) r = mid;
        else l = mid + 1;
    }
    m = l;
    int rr = 0;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        a [i] = aa [i];
        wra [ a [i] ] = i;
    }
    for ( int i = 1 ; i <= n ; i ++ )
    {
        dis [i] = i;
        wrdis [ dis [i] ] = i;
    }
    for ( int j = m - 1 ; j >= 0 ; j -- )
    {
        swap ( dis [ x [j] ] , dis [ y [j] ] );
        swap ( wrdis [ x [j] ] , wrdis [ y [j] ] );
    }
    set < int > dq;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        if ( dis [i] != a [i] ) dq . insert ( i );
    }
    for ( int i = 1 ; i <= m ; i ++ )
    {
        if ( dq . size () == 0 ) return 1;
        swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
        swap ( dis [ x [ i - 1 ] ] , dis [ y [ i - 1 ] ] );
        swap ( wrdis [ x [ i - 1 ] ] , wrdis [ y [ i - 1 ] ] );
        swap ( wra [ a [ x [ i - 1 ] ] ] , wra [ a [ y [ i - 1 ] ] ] );


        int x = * dq . begin ();
        dq . erase ( dq . begin () );

        int y = wra [ dis [x] ];

        swap ( a [x] , a [y] );
        swap ( wra [ a [x] ] , wra [ a [y] ] );
        P [ i - 1 ] = x - 1 , Q [ i - 1 ] = y - 1;
        rr ++;

        if ( dis [y] == a [y] ) dq . erase ( dq . lower_bound ( y ) );
    }
    return rr;
}


Compilation message

sorting.cpp: In function 'bool ck(int)':
sorting.cpp:39:13: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   39 |         int x = * dq . begin ();
      |             ^
sorting.cpp:5:40: note: shadowed declaration is here
    5 | int n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005];
      |                                        ^
sorting.cpp:42:13: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   42 |         int y = wra [ dis [x] ];
      |             ^
sorting.cpp:5:53: note: shadowed declaration is here
    5 | int n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005];
      |                                                     ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:106:13: warning: declaration of 'x' shadows a global declaration [-Wshadow]
  106 |         int x = * dq . begin ();
      |             ^
sorting.cpp:5:40: note: shadowed declaration is here
    5 | int n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005];
      |                                        ^
sorting.cpp:109:13: warning: declaration of 'y' shadows a global declaration [-Wshadow]
  109 |         int y = wra [ dis [x] ];
      |             ^
sorting.cpp:5:53: note: shadowed declaration is here
    5 | int n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005];
      |                                                     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Incorrect 0 ms 340 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -