답안 #108756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
108756 2019-05-01T14:54:53 Z PeppaPig 정렬하기 (IOI15_sorting) C++14
100 / 100
338 ms 21676 KB
#include "sorting.h"
#include <bits/stdc++.h>

#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 2e5+5;

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    auto solve = [&](int k) {
        vector<int> v(s, s + n), pos(n);
        vector<pii> ret;
        for(int i = 0; i < k; i++) swap(v[x[i]], v[y[i]]);
        for(int i = 0; i < n; i++) pos[v[i]] = i;
        for(int i = 0; i < n; i++) if(v[i] != i) {
            ret.emplace_back(v[i], i);
            int now = v[i];
            swap(v[i], v[pos[i]]);
            swap(pos[now], pos[i]);
        }
        return ret;
    };

    int l = 0, r = m;
    while(l < r) {
        int mid = (l + r) >> 1;
        if(solve(mid).size() <= mid) r = mid;
        else l = mid+1;
    }

    vector<pii> ans = solve(r);
    vector<int> pos(n);
    for(int i = 0; i < n; i++) pos[s[i]] = i;
    for(int i = 0; i < r; i++) {
        swap(pos[s[x[i]]], pos[s[y[i]]]);
        swap(s[x[i]], s[y[i]]);

        if(i < ans.size()) {
            int a, b; tie(a, b) = ans[i];
            p[i] = pos[a], q[i] = pos[b];
            swap(s[pos[a]], s[pos[b]]);
            swap(pos[a], pos[b]);
        }
    }
    for(int i = ans.size(); i < r; i++) p[i] = 0, q[i] = 0;

    return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:30:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(solve(mid).size() <= mid) r = mid;
            ~~~~~~~~~~~~~~~~~~^~~~~~
sorting.cpp:41:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(i < ans.size()) {
            ~~^~~~~~~~~~~~
sorting.cpp:48:25: warning: conversion to 'int' from 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' may alter its value [-Wconversion]
     for(int i = ans.size(); i < r; i++) p[i] = 0, q[i] = 0;
                 ~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 6 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 6 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 6 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 4 ms 512 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 209 ms 19076 KB Output is correct
16 Correct 254 ms 19504 KB Output is correct
17 Correct 288 ms 20596 KB Output is correct
18 Correct 87 ms 18040 KB Output is correct
19 Correct 175 ms 19648 KB Output is correct
20 Correct 162 ms 20336 KB Output is correct
21 Correct 212 ms 20332 KB Output is correct
22 Correct 232 ms 15816 KB Output is correct
23 Correct 242 ms 21176 KB Output is correct
24 Correct 233 ms 20888 KB Output is correct
25 Correct 252 ms 20584 KB Output is correct
26 Correct 201 ms 18960 KB Output is correct
27 Correct 169 ms 18488 KB Output is correct
28 Correct 242 ms 20696 KB Output is correct
29 Correct 229 ms 20404 KB Output is correct
30 Correct 152 ms 17380 KB Output is correct
31 Correct 243 ms 20852 KB Output is correct
32 Correct 214 ms 20736 KB Output is correct
33 Correct 236 ms 20884 KB Output is correct
34 Correct 241 ms 20948 KB Output is correct
35 Correct 187 ms 19348 KB Output is correct
36 Correct 53 ms 15992 KB Output is correct
37 Correct 276 ms 21676 KB Output is correct
38 Correct 258 ms 20844 KB Output is correct
39 Correct 338 ms 20752 KB Output is correct