답안 #288420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
288420 2020-09-01T13:29:54 Z Kastanda 정렬하기 (IOI15_sorting) C++11
100 / 100
206 ms 17832 KB
// M
#include<bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int N = 200005 * 3;
int n, m, S[N], X[N], Y[N];
int A[N], M[N], Rev[N];
inline bool Solve(int md)
{
        for (int i = 0; i < n; i ++)
                A[i] = S[i], M[i] = 0;
        for (int i = 0; i < md; i ++)
                swap(A[X[i]], A[Y[i]]);

        int Cnt = 0;
        for (int i = 0; i < n; i ++)
                if (!M[i])
                {
                        int nw = i;
                        while (!M[nw])
                                M[nw] = 1, nw = A[nw];
                        Cnt ++;
                }
        Cnt = n - Cnt;
        return Cnt <= md;
}
int findSwapPairs(int _n, int _S[], int _m, int _X[], int _Y[], int P[], int Q[])
{
        n = _n; m = _m;
        for (int i = 0; i < n; i ++)
                S[i] = _S[i];
        for (int i = 0; i < m; i ++)
                X[i] = _X[i], Y[i] = _Y[i];

        {
                bool fail = 0;
                for (int i = 1; i < n; i ++)
                        if (S[i] < S[i - 1])
                                fail = 1;
                if (!fail)
                        return 0;
        }

        int le = 0, ri = m, md;
        while (ri - le > 1)
        {
                md = (le + ri) >> 1;
                if (Solve(md))
                        ri = md;
                else
                        le = md;
        }

        assert(Solve(ri));

        for (int i = 0; i < n; i ++)
                A[i] = S[i], M[i] = 0;
        for (int i = 0; i < ri; i ++)
                swap(A[X[i]], A[Y[i]]);

        vector < pair < int , int > > E;
        for (int i = 0; i < n; i ++)
                if (!M[i])
                {
                        int nw = i;
                        vector < int > vec;
                        while (!M[nw])
                        {
                                vec.push_back(nw);
                                M[nw] = 1;
                                nw = A[nw];
                        }
                        reverse(vec.begin(), vec.end());
                        for (int j = 1; j < (int)vec.size(); j ++)
                                E.push_back(make_pair(vec[j - 1], vec[j]));
                }
        assert(E.size() <= ri);
        for (int i = 0; i < n; i ++)
                A[i] = S[i];
        for (int i = 0; i < n; i ++)
                Rev[A[i]] = i;

        for (int i = 0; i < ri; i ++)
        {
                swap(A[X[i]], A[Y[i]]);
                swap(Rev[A[X[i]]], Rev[A[Y[i]]]);
                if (!E.size())
                {
                        P[i] = Q[i] = 0;
                        continue;
                }

                int a = E.back().first;
                int b = E.back().second;
                E.pop_back();

                int ra = Rev[a];
                int rb = Rev[b];

                P[i] = ra;
                Q[i] = rb;

                swap(A[ra], A[rb]);
                swap(Rev[A[ra]], Rev[A[rb]]);
        }
        return ri;


}

Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from sorting.cpp:2:
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:77:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   77 |         assert(E.size() <= ri);
      |                ~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 1 ms 672 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 163 ms 16224 KB Output is correct
16 Correct 175 ms 16548 KB Output is correct
17 Correct 192 ms 17368 KB Output is correct
18 Correct 44 ms 10488 KB Output is correct
19 Correct 163 ms 15404 KB Output is correct
20 Correct 175 ms 15936 KB Output is correct
21 Correct 172 ms 16072 KB Output is correct
22 Correct 162 ms 17644 KB Output is correct
23 Correct 182 ms 17696 KB Output is correct
24 Correct 195 ms 17776 KB Output is correct
25 Correct 193 ms 17452 KB Output is correct
26 Correct 173 ms 15956 KB Output is correct
27 Correct 147 ms 15300 KB Output is correct
28 Correct 195 ms 17492 KB Output is correct
29 Correct 198 ms 16848 KB Output is correct
30 Correct 118 ms 14700 KB Output is correct
31 Correct 206 ms 17228 KB Output is correct
32 Correct 189 ms 17408 KB Output is correct
33 Correct 195 ms 17832 KB Output is correct
34 Correct 201 ms 17380 KB Output is correct
35 Correct 161 ms 15340 KB Output is correct
36 Correct 65 ms 13560 KB Output is correct
37 Correct 196 ms 17460 KB Output is correct
38 Correct 191 ms 17168 KB Output is correct
39 Correct 191 ms 16680 KB Output is correct