Submission #245619

# Submission time Handle Problem Language Result Execution time Memory
245619 2020-07-07T01:24:08 Z thecodingwizard Swap (BOI16_swap) C++11
68 / 100
1000 ms 256760 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 200000;

int n, A[maxn+10];
vector<int> dp[maxn+10][36], tmp1, tmp2;
bool v[maxn+10][40];

void merge(vector<int> &res, int root, vector<int> &L, vector<int> &R) {
    res.push_back(root);
    for (int len = 1, i = 0, j = 0; i < L.size() || j < R.size(); i += len, j += len, len *= 2) {
        for (int k = 0; k < len && i+k<L.size(); k++) res.push_back(L[i+k]);
        for (int k = 0; k < len && i+k<R.size(); k++) res.push_back(R[i+k]);
    }
}

int main() {
    cin.tie(0); ios_base::sync_with_stdio(0);
    cin >> n; for (int i = 1; i <= n; i++) cin >> A[i];
    v[1][0] = 1;
    for (int i = 1; 2*i+1 <= n; i++) {
        for (int j = 0; j < 36; j++) {
            if (!v[i][j]) continue;
            int idx = i>>(j/2);
            if (j&1) idx *= 2;
            int cur = A[idx];
            int l = i*2, r = i*2+1;
            int L = A[l], R = A[r];

            v[2*i][0] = v[2*i+1][0] = true;

            if (L<cur&&L<R) {
                v[2*i][j+2] = true;
            }
            if (R<cur&&R<L) {
                v[2*i+1][3] = true;
                v[2*i][j+2] = true;
                v[2*i+1][j+2] = true;
            }
        }
    }
    for (int i = n; i; i--) {
        int l = i*2, r = i*2+1;
        for (int j = 0; j < 36; j++) {
            if (!v[i][j]) continue;
            int idx = i>>(j/2);
            if (j&1) idx *= 2;
            int cur = A[idx];

            if (l > n && r > n) {
                dp[i][j] = {cur};
                continue;
            } else if (r > n) {
                dp[i][j] = {min(A[l],cur),max(A[l],cur)};
                continue;
            }

            int L = A[l], R = A[r];
            // no swap
            if (cur < L && cur < R) {
                merge(dp[i][j], cur, dp[l][0], dp[r][0]);
                continue;
            }

            // swap left
            if (cur > L && L < R) {
                merge(dp[i][j], L, dp[l][j+2], dp[r][0]);
                continue;
            }

            if (R < cur && R < L) {
                tmp1 = {}, tmp2 = {};
                // swap right
                merge(tmp1, R, dp[l][0], dp[r][j+2]);
                // swap both
                merge(tmp2, R, dp[l][j+2], dp[r][3]);
                dp[i][j] = min(tmp1, tmp2);
            }
        }

        if (l <= n) {
            for (int j = 0; j < 36; j++) if (v[l][j]) vector<int>().swap(dp[l][j]);
        }
        if (r <= n) {
            for (int j = 0; j < 36; j++) if (v[r][j]) vector<int>().swap(dp[r][j]);
        }
    }
    for (int v : dp[1][0]) cout << v << " ";
    cout << "\n";
    return 0;
}

Compilation message

swap.cpp: In function 'void merge(std::vector<int>&, int, std::vector<int>&, std::vector<int>&)':
swap.cpp:13:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int len = 1, i = 0, j = 0; i < L.size() || j < R.size(); i += len, j += len, len *= 2) {
                                     ~~^~~~~~~~~~
swap.cpp:13:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int len = 1, i = 0, j = 0; i < L.size() || j < R.size(); i += len, j += len, len *= 2) {
                                                     ~~^~~~~~~~~~
swap.cpp:14:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int k = 0; k < len && i+k<L.size(); k++) res.push_back(L[i+k]);
                                    ~~~^~~~~~~~~
swap.cpp:15:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int k = 0; k < len && i+k<R.size(); k++) res.push_back(R[i+k]);
                                    ~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 113 ms 169464 KB Output is correct
2 Correct 117 ms 169464 KB Output is correct
3 Correct 100 ms 169464 KB Output is correct
4 Correct 102 ms 169488 KB Output is correct
5 Correct 116 ms 169464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 169464 KB Output is correct
2 Correct 117 ms 169464 KB Output is correct
3 Correct 100 ms 169464 KB Output is correct
4 Correct 102 ms 169488 KB Output is correct
5 Correct 116 ms 169464 KB Output is correct
6 Correct 114 ms 169464 KB Output is correct
7 Correct 103 ms 169592 KB Output is correct
8 Correct 110 ms 169592 KB Output is correct
9 Correct 105 ms 169464 KB Output is correct
10 Correct 102 ms 169464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 169464 KB Output is correct
2 Correct 117 ms 169464 KB Output is correct
3 Correct 100 ms 169464 KB Output is correct
4 Correct 102 ms 169488 KB Output is correct
5 Correct 116 ms 169464 KB Output is correct
6 Correct 114 ms 169464 KB Output is correct
7 Correct 103 ms 169592 KB Output is correct
8 Correct 110 ms 169592 KB Output is correct
9 Correct 105 ms 169464 KB Output is correct
10 Correct 102 ms 169464 KB Output is correct
11 Correct 105 ms 169696 KB Output is correct
12 Correct 104 ms 169592 KB Output is correct
13 Correct 106 ms 169592 KB Output is correct
14 Correct 104 ms 169720 KB Output is correct
15 Correct 107 ms 169720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 169464 KB Output is correct
2 Correct 117 ms 169464 KB Output is correct
3 Correct 100 ms 169464 KB Output is correct
4 Correct 102 ms 169488 KB Output is correct
5 Correct 116 ms 169464 KB Output is correct
6 Correct 114 ms 169464 KB Output is correct
7 Correct 103 ms 169592 KB Output is correct
8 Correct 110 ms 169592 KB Output is correct
9 Correct 105 ms 169464 KB Output is correct
10 Correct 102 ms 169464 KB Output is correct
11 Correct 105 ms 169696 KB Output is correct
12 Correct 104 ms 169592 KB Output is correct
13 Correct 106 ms 169592 KB Output is correct
14 Correct 104 ms 169720 KB Output is correct
15 Correct 107 ms 169720 KB Output is correct
16 Correct 157 ms 174072 KB Output is correct
17 Correct 177 ms 174072 KB Output is correct
18 Correct 161 ms 174072 KB Output is correct
19 Correct 287 ms 189048 KB Output is correct
20 Correct 305 ms 188792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 169464 KB Output is correct
2 Correct 117 ms 169464 KB Output is correct
3 Correct 100 ms 169464 KB Output is correct
4 Correct 102 ms 169488 KB Output is correct
5 Correct 116 ms 169464 KB Output is correct
6 Correct 114 ms 169464 KB Output is correct
7 Correct 103 ms 169592 KB Output is correct
8 Correct 110 ms 169592 KB Output is correct
9 Correct 105 ms 169464 KB Output is correct
10 Correct 102 ms 169464 KB Output is correct
11 Correct 105 ms 169696 KB Output is correct
12 Correct 104 ms 169592 KB Output is correct
13 Correct 106 ms 169592 KB Output is correct
14 Correct 104 ms 169720 KB Output is correct
15 Correct 107 ms 169720 KB Output is correct
16 Correct 157 ms 174072 KB Output is correct
17 Correct 177 ms 174072 KB Output is correct
18 Correct 161 ms 174072 KB Output is correct
19 Correct 287 ms 189048 KB Output is correct
20 Correct 305 ms 188792 KB Output is correct
21 Correct 363 ms 187768 KB Output is correct
22 Correct 358 ms 187840 KB Output is correct
23 Correct 353 ms 188732 KB Output is correct
24 Execution timed out 1069 ms 256760 KB Time limit exceeded
25 Halted 0 ms 0 KB -