답안 #703024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
703024 2023-02-25T15:59:06 Z 406 Swap (BOI16_swap) C++17
48 / 100
1000 ms 17012 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 2e5 + 5;
const int L = 20;
const long long INF = 1ll << 60;
map<int, vector<int>> dp[N];
int n, a[4 * N];

void merge(vector<int> &a, const vector<int> b, const vector<int> c) {
    assert(b.size() && c.size());
    assert(b.size() == c.size());
    int ptr = 0;
    for (int i = 0; ptr < b.size(); i++) {
        for (int j = 0; j < (1 << i); j++) {
            a.push_back(b[j + ptr]);
        }
        for (int j = 0; j < (1 << i); j++) {
            a.push_back(c[j + ptr]);
        }
        ptr += 1 << i;
    }
}   

void DP(int i, int j) {
    if (i >= n / 2) {
        dp[i][j] = {j};
        return;
    }
    int r = a[2 * i + 1];
    int l = a[2 * i];
    if (j <= l && j <= r) {
        DP(i * 2, l);
        DP(i * 2 + 1, r);
        vector<int> re{j};
        merge(re, dp[i << 1][l], dp[i << 1 | 1][r]);
        dp[i][j] = re;
        return;
    }
    
    if (j <= l) {
        DP(i * 2, j);
        DP(i * 2 + 1, j);
        DP(i * 2, l);
        DP(i * 2 + 1, l);
        vector<int> re{r};
        merge(re, dp[i * 2][l], dp[i * 2 + 1][j]);

        vector<int> re2{r};
        merge(re2, dp[i * 2][j], dp[i * 2 + 1][l]);

        dp[i][j] = min(re, re2);
        return;
    }
    if (j <= r) {
        DP(i * 2, j);
        DP(i * 2 + 1, r);
        vector<int> re{l};
        merge(re, dp[i << 1][j], dp[i << 1 | 1][r]);
        dp[i][j] = re;
        return;
    }
    DP(i * 2, j);
    DP(i * 2 + 1, j);
    DP(i * 2, r);
    DP(i * 2 + 1, r);
    DP(i * 2, l);
    DP(i * 2 + 1, l);

    vector<int> re{r};
    merge(re, dp[i * 2][l], dp[i * 2 + 1][j]);

    vector<int> re2{r};
    merge(re2, dp[i * 2][j], dp[i * 2 + 1][l]);

    vector<int> re3{l}; 
    merge(re3, dp[i * 2][j], dp[i * 2 + 1][r]);

    re = min(re, re2);
    re = min(re, re3);
    dp[i][j] = re;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) 
        cin >> a[i];

    while (n & (n - 1)) {
        a[++n] = INF;
    }

    DP(1, a[1]);
    for (auto u: dp[1][a[1]]) if (u != INF)
        cout << u << ' ';
    cout << '\n';

    return 0;
}

Compilation message

swap.cpp: In function 'void merge(std::vector<long long int>&, std::vector<long long int>, std::vector<long long int>)':
swap.cpp:14:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; ptr < b.size(); i++) {
      |                     ~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9728 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9672 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9728 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9672 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 23 ms 10460 KB Output is correct
12 Correct 48 ms 10672 KB Output is correct
13 Correct 15 ms 10400 KB Output is correct
14 Correct 899 ms 12024 KB Output is correct
15 Correct 769 ms 11808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9728 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9672 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 23 ms 10460 KB Output is correct
12 Correct 48 ms 10672 KB Output is correct
13 Correct 15 ms 10400 KB Output is correct
14 Correct 899 ms 12024 KB Output is correct
15 Correct 769 ms 11808 KB Output is correct
16 Execution timed out 1068 ms 17012 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9728 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9672 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 23 ms 10460 KB Output is correct
12 Correct 48 ms 10672 KB Output is correct
13 Correct 15 ms 10400 KB Output is correct
14 Correct 899 ms 12024 KB Output is correct
15 Correct 769 ms 11808 KB Output is correct
16 Execution timed out 1068 ms 17012 KB Time limit exceeded
17 Halted 0 ms 0 KB -