답안 #432524

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
432524 2021-06-18T10:51:17 Z snasibov05 정렬하기 (IOI15_sorting) C++14
74 / 100
294 ms 524292 KB
#include "sorting.h"
#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {

    vector<vector<int>> v(m+1, vector<int>(n));
    vector<vector<int>> idx(m+1, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        v[0][i] = s[i];
        idx[0][s[i]] = i;
    }

    if (is_sorted(v[0].begin(), v[0].end())) return 0;

    for (int i = 1; i <= m; ++i) {
        v[i] = v[i-1];
        idx[i] = idx[i-1];
        swap(idx[i][v[i][x[i-1]]], idx[i][v[i][y[i-1]]]);
        swap(v[i][x[i-1]], v[i][y[i-1]]);

        vector<int> final = idx[i];
        vector<int> arr = v[i];
        vector<pii> swaps;

        for (int j = 0; j < n; ++j) {
            if (final[j] == j) continue;
            int k = final[j];
            swaps.pb({j, k});
            final[arr[j]] = k;
            arr[k] = arr[j];
            arr[j] = j;
        }

        if (swaps.size() > i) continue;

        arr = v[i];

        for (int j = 0; j < m; ++j) {
            p[i] = q[i] = 0;
        }

        int k = swaps.size();
        for (int l = 1; l <= k; ++l){
            if (is_sorted(v[l].begin(), v[l].end())) return l;

            int a = idx[l][arr[swaps[l-1].f]];
            int b = idx[l][arr[swaps[l-1].s]];
            p[l-1] = a, q[l-1] = b;
            swap(idx[l][arr[swaps[l-1].f]], idx[l][arr[swaps[l-1].s]]);
            swap(v[l][a], v[l][b]);
            swap(arr[swaps[l-1].f], arr[swaps[l-1].s]);

            if (is_sorted(v[l].begin(), v[l].end())) return l;

            int j = l+1;
            v[j] = v[j-1];
            idx[j] = idx[j-1];
            swap(idx[j][v[j][x[j-1]]], idx[j][v[j][y[j-1]]]);
            swap(v[j][x[j-1]], v[j][y[j-1]]);

        }

        return i;
    }

    assert(false);

}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:43:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   43 |         if (swaps.size() > i) continue;
      |             ~~~~~~~~~~~~~^~~
sorting.cpp:51:27: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   51 |         int k = swaps.size();
      |                 ~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 3 ms 2636 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 38 ms 55708 KB Output is correct
22 Correct 35 ms 54852 KB Output is correct
23 Correct 36 ms 59340 KB Output is correct
24 Correct 35 ms 56516 KB Output is correct
25 Correct 39 ms 59288 KB Output is correct
26 Correct 37 ms 54964 KB Output is correct
27 Correct 33 ms 54720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 76880 KB Output is correct
2 Correct 107 ms 90136 KB Output is correct
3 Correct 100 ms 81388 KB Output is correct
4 Correct 48 ms 90728 KB Output is correct
5 Correct 49 ms 94044 KB Output is correct
6 Correct 48 ms 88868 KB Output is correct
7 Correct 64 ms 94880 KB Output is correct
8 Correct 105 ms 85728 KB Output is correct
9 Correct 113 ms 88512 KB Output is correct
10 Correct 111 ms 89024 KB Output is correct
11 Correct 120 ms 94544 KB Output is correct
12 Correct 63 ms 93716 KB Output is correct
13 Correct 88 ms 91144 KB Output is correct
14 Correct 44 ms 86044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 76880 KB Output is correct
2 Correct 107 ms 90136 KB Output is correct
3 Correct 100 ms 81388 KB Output is correct
4 Correct 48 ms 90728 KB Output is correct
5 Correct 49 ms 94044 KB Output is correct
6 Correct 48 ms 88868 KB Output is correct
7 Correct 64 ms 94880 KB Output is correct
8 Correct 105 ms 85728 KB Output is correct
9 Correct 113 ms 88512 KB Output is correct
10 Correct 111 ms 89024 KB Output is correct
11 Correct 120 ms 94544 KB Output is correct
12 Correct 63 ms 93716 KB Output is correct
13 Correct 88 ms 91144 KB Output is correct
14 Correct 44 ms 86044 KB Output is correct
15 Runtime error 294 ms 524292 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -