답안 #471758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471758 2021-09-10T17:21:37 Z blue 정렬하기 (IOI15_sorting) C++17
100 / 100
209 ms 24076 KB
#include "sorting.h"
#include <vector>
#include <queue>
#include <iostream>
using namespace std;


const int maxN = 200'000;

int N;
vector<int> S(maxN);

int M;
int* X;
int* Y;

int* P;
int* Q;

vector<int> T(maxN);
vector<bool> visit(maxN);

int binary_search(int l, int r)
{
    // cerr << "search " << l << ' ' << r << '\n';
    if(l == r)
    {
        vector<int> act1, act2;

        T = S;
        for(int j = 0; j < l; j++)
            swap(T[ X[j] ], T[ Y[j] ]);

        // cerr << "final: ";
        // for(int i = 0; i < N; i++) cerr << T[i] << ' ';
        // cerr << '\n';

        vector<int> num1, num2;

        for(int i = 0; i < N; i++)
        {
            int u, v;
            u = i;
            v = T[u];
            // cerr << u << v << '\n';
            while(1)
            {
                if(u == v) break;

                swap(T[u], T[v]);
                act1.push_back(u);
                act2.push_back(v);

                num1.push_back(T[u]);
                num2.push_back(T[v]);

                // cerr << "act swap " << u << ' ' << v << '\n';

                u = T[u];
                v = T[u];
            }
        }


        vector<int> real(N);
        for(int i = 0; i < N; i++) real[i] = i;

        vector<int> Z(N);
        for(int i = 0; i < N; i++) Z[i] = S[i];
        vector<int> Zpos(N);
        for(int i = 0; i < N; i++) Zpos[ Z[i] ] = i;

        for(int j = 0; j < l; j++)
        {
            swap(Z[ X[j] ], Z[ Y[j] ]);
            swap(Zpos[ Z[ X[j] ] ], Zpos[ Z[ Y[j] ] ]);

            swap(Z[ Zpos[ num1[j] ] ], Z[ Zpos[ num2[j] ] ]);
            swap(Zpos[ num1[j] ], Zpos[ num2[j] ]);
            P[j] = Zpos[ num1[j] ];
            Q[j] = Zpos[ num2[j] ];
        }

        // for(int j = l-1; j >= 0; j--)
        // {
        //     // cerr << "j = " << j << '\n';
        //     // cerr << act1[j] << ' ' << act2[j] << '\n';
        //
        //     // for(int i = 0; i < N; i++) cerr << real[i] << ' ';
        //     // cerr << '\n';
        //     // cerr << "done\n";
        //
        //     P[j] = real[ act1[j] ];
        //     Q[j] = real[ act2[j] ];
        //     swap(real[ X[j] ], real[ Y[j] ]);
        // }


        // cerr << "sequence after unadjusted sorting: ";
        // for(int i = 0; i < N; i++) cerr << T[i] << ' ';
        // cerr << '\n';

        vector<int> fin = S;
        for(int j = 0; j < l; j++)
        {
            swap(fin[ X[j] ], fin[ Y[j] ]);
            swap(fin[ P[j] ], fin[ Q[j] ]);
        }
        // cerr << "sequence after sorting: ";
        // for(int i = 0; i < N; i++) cerr << fin[i] << ' ';
        // cerr << '\n';


        return l;
    }
    else
    {
        int m = (l+r)/2;

        T = S;

        for(int j = 0; j < m; j++)
        {
            swap(T[ X[j] ], T[ Y[j] ]);
        }

        for(int i = 0; i < N; i++) visit[i] = 0;
        int ct = 0;
        for(int i = 0; i < N; i++)
        {
            if(visit[i]) continue;
            ct++;

            visit[i] = 1;
            for(int j = T[i]; j != i; j = T[j])
                visit[j] = 1;
        }

        if(N - ct <= m)
            return binary_search(l, m);
        else
            return binary_search(m+1, r);
    }
}

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++) S[i] = S_[i];

    M = M_;
    X = X_;
    Y = Y_;

    P = P_;
    Q = Q_;

    return binary_search(0, M);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 3 ms 2892 KB Output is correct
23 Correct 4 ms 2892 KB Output is correct
24 Correct 3 ms 2928 KB Output is correct
25 Correct 4 ms 2812 KB Output is correct
26 Correct 3 ms 2892 KB Output is correct
27 Correct 3 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2804 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 3 ms 2808 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 3 ms 2764 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 4 ms 2764 KB Output is correct
14 Correct 3 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2804 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 3 ms 2808 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 3 ms 2764 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 4 ms 2764 KB Output is correct
14 Correct 3 ms 2764 KB Output is correct
15 Correct 161 ms 21532 KB Output is correct
16 Correct 170 ms 21920 KB Output is correct
17 Correct 184 ms 22972 KB Output is correct
18 Correct 76 ms 17856 KB Output is correct
19 Correct 150 ms 21120 KB Output is correct
20 Correct 154 ms 21848 KB Output is correct
21 Correct 151 ms 21808 KB Output is correct
22 Correct 156 ms 18328 KB Output is correct
23 Correct 189 ms 23760 KB Output is correct
24 Correct 186 ms 23500 KB Output is correct
25 Correct 202 ms 23228 KB Output is correct
26 Correct 155 ms 21876 KB Output is correct
27 Correct 160 ms 21120 KB Output is correct
28 Correct 184 ms 23308 KB Output is correct
29 Correct 190 ms 22820 KB Output is correct
30 Correct 108 ms 20300 KB Output is correct
31 Correct 176 ms 23144 KB Output is correct
32 Correct 172 ms 23072 KB Output is correct
33 Correct 183 ms 23392 KB Output is correct
34 Correct 188 ms 23328 KB Output is correct
35 Correct 148 ms 20996 KB Output is correct
36 Correct 68 ms 19020 KB Output is correct
37 Correct 209 ms 24076 KB Output is correct
38 Correct 185 ms 23240 KB Output is correct
39 Correct 188 ms 23324 KB Output is correct