제출 #419217

#제출 시각아이디문제언어결과실행 시간메모리
419217someone정렬하기 (IOI15_sorting)C++14
100 / 100
209 ms25752 KiB
#include <bits/stdc++.h>
#include "sorting.h"
//#define int long long
using namespace std;

const int N = 2e5 + 10, M = N * 3;

int n, a[N], b[N], s[M][2], pos[N];

int dicho(int deb, int fin) {
    if(deb + 1 == fin)
        return deb;
    int med = (deb + fin) / 2 - 1;
    for(int i = 0; i < n; i++)
        b[i] = a[i];
    for(int i = 0; i < med; i++)
        swap(b[s[i][0]], b[s[i][1]]);
    int t = 0;
    for(int i = 0; i < n; i++) {
        while(b[i] != i) {
            t++;
            swap(b[i], b[b[i]]);
        }
    }
    if(t <= med)
        return dicho(deb, med+1);
    return dicho(med+1, fin);
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    for(int i = 0; i < n; i++)
        a[i] = S[i];
    for(int i = 0; i < M; i++) {
        s[i][0] = X[i];
        s[i][1] = Y[i];
    }
    int etape = dicho(0, M);
    for(int i = 0; i < etape; i++)
        P[i] = Q[i] = 0;
    for(int i = 0; i < n; i++)
        b[i] = a[i];
    for(int i = 0; i < etape; i++)
        swap(b[s[i][0]], b[s[i][1]]);
    for(int i = 0; i < n; i++)
        pos[a[i]] = i;

    int j = 0;
    for(int i = 0; i < n; i++) {
        while(b[i] != i) {
            swap(pos[a[s[j][0]]], pos[a[s[j][1]]]);
            swap(a[s[j][0]], a[s[j][1]]);
            P[j] = pos[b[i]];
            Q[j] = pos[b[b[i]]];
            swap(pos[b[i]], pos[b[b[i]]]);
            swap(a[pos[b[i]]], a[pos[b[b[i]]]]);
            swap(b[i], b[b[i]]);
            j++;
        }
    }
    return etape;
}

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:30:39: warning: declaration of 'M' shadows a global declaration [-Wshadow]
   30 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:6:25: note: shadowed declaration is here
    6 | const int N = 2e5 + 10, M = N * 3;
      |                         ^
sorting.cpp:30:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   30 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:6:11: note: shadowed declaration is here
    6 | const int N = 2e5 + 10, M = N * 3;
      |           ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...