Submission #322243

# Submission time Handle Problem Language Result Execution time Memory
322243 2020-11-14T10:24:50 Z dolphingarlic Swap (BOI16_swap) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

int a[200002];
bool needed[200002];
vector<int> dp[200002][18][2], tmp;

void merge(vector<int> &ret, vector<int> &l, vector<int> &r) {
    for (int i = 0, j = 1; i < l.size(); i += j, j <<= 1) {
        for (int k = i; k < i + j && k < l.size(); k++) ret.push_back(l[k]);
        for (int k = i; k < i + j && k < r.size(); k++) ret.push_back(r[k]);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", a + i);

    for (int node = n; node; node--) {
        for (int i = 0; node >> i; i++) for (int j : {0, 1}) {
            if (node * 2 >= n || !(((node >> i + 1) << 1) + j)) continue;
            int val = a[((node >> i + 1) << 1) + j];
            int mn = min({val, a[2 * node], a[2 * node + 1]});
            if (mn == val) {
                needed[2 * node][0][0] = needed[2 * node + 1][0][1] = true;
            } else if (mn == a[2 * node]) {
                needed[2 * node][i + 1][j] = needed[2 * node + 1][0][1] = true;
            } else {
                needed[2 * node][0][0] = needed[2 * node][i + 1][j] = 1;
                needed[2 * node + 1][0][0] = needed[2 * node + 1][i + 1][j] = 1;
            }
        }
    }


    for (int node = n; node; node--) {
        for (int i = 0; node >> i; i++) for (int j : {0, 1}) {
            if (!needed[node][i][j]) continue;
            int val = a[((node >> i + 1) << 1) + j];

            if (node * 2 > n) dp[node][i][j] = {val};
            else if (node * 2 == n) {
                if (val < a[2 * node]) dp[node][i][j] = {val, a[2 * node]};
                else dp[node][i][j] = {a[2 * node], val};
            } else {
                int mn = min({val, a[2 * node], a[2 * node + 1]});
                if (mn == val) {
                    dp[node][i][j] = {val};
                    merge(dp[node][i][j], dp[2 * node][0][0], dp[2 * node + 1][0][1]);
                } else if (mn == a[2 * node]) {
                    dp[node][i][j] = {a[2 * node]};
                    merge(dp[node][i][j], dp[2 * node][i + 1][j], dp[2 * node + 1][0][1]);
                } else {
                    dp[node][i][j] = tmp = {a[2 * node + 1]};
                    merge(dp[node][i][j], dp[2 * node][0][0], dp[2 * node + 1][i + 1][j]);
                    merge(tmp, dp[2 * node][i + 1][j], dp[2 * node + 1][0][0]);
                    dp[node][i][j] = min(dp[node][i][j], tmp);
                }
                vector<int>().swap(dp[2 * node][i + 1][j]);
                vector<int>().swap(dp[2 * node + 1][i + 1][j]);
            }
        }
        if (node * 2 <= n) {
            vector<int>().swap(dp[node * 2][0][0]);
            vector<int>().swap(dp[node * 2][0][1]);
            vector<int>().swap(dp[node * 2 + 1][0][0]);
            vector<int>().swap(dp[node * 2 + 1][0][1]);
        }
    }

    for (int i : dp[1][0][1]) printf("%d ", i);
    return 0;
}

Compilation message

swap.cpp: In function 'void merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:9:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |     for (int i = 0, j = 1; i < l.size(); i += j, j <<= 1) {
      |                            ~~^~~~~~~~~~
swap.cpp:10:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |         for (int k = i; k < i + j && k < l.size(); k++) ret.push_back(l[k]);
      |                                      ~~^~~~~~~~~~
swap.cpp:11:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |         for (int k = i; k < i + j && k < r.size(); k++) ret.push_back(r[k]);
      |                                      ~~^~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:22:48: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |             if (node * 2 >= n || !(((node >> i + 1) << 1) + j)) continue;
      |                                              ~~^~~
swap.cpp:23:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |             int val = a[((node >> i + 1) << 1) + j];
      |                                   ~~^~~
swap.cpp:26:35: error: invalid types 'bool[int]' for array subscript
   26 |                 needed[2 * node][0][0] = needed[2 * node + 1][0][1] = true;
      |                                   ^
swap.cpp:26:64: error: invalid types 'bool[int]' for array subscript
   26 |                 needed[2 * node][0][0] = needed[2 * node + 1][0][1] = true;
      |                                                                ^
swap.cpp:28:39: error: invalid types 'bool[int]' for array subscript
   28 |                 needed[2 * node][i + 1][j] = needed[2 * node + 1][0][1] = true;
      |                                       ^
swap.cpp:28:68: error: invalid types 'bool[int]' for array subscript
   28 |                 needed[2 * node][i + 1][j] = needed[2 * node + 1][0][1] = true;
      |                                                                    ^
swap.cpp:30:35: error: invalid types 'bool[int]' for array subscript
   30 |                 needed[2 * node][0][0] = needed[2 * node][i + 1][j] = 1;
      |                                   ^
swap.cpp:30:64: error: invalid types 'bool[int]' for array subscript
   30 |                 needed[2 * node][0][0] = needed[2 * node][i + 1][j] = 1;
      |                                                                ^
swap.cpp:31:39: error: invalid types 'bool[int]' for array subscript
   31 |                 needed[2 * node + 1][0][0] = needed[2 * node + 1][i + 1][j] = 1;
      |                                       ^
swap.cpp:31:72: error: invalid types 'bool[int]' for array subscript
   31 |                 needed[2 * node + 1][0][0] = needed[2 * node + 1][i + 1][j] = 1;
      |                                                                        ^
swap.cpp:39:32: error: invalid types 'bool[int]' for array subscript
   39 |             if (!needed[node][i][j]) continue;
      |                                ^
swap.cpp:40:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |             int val = a[((node >> i + 1) << 1) + j];
      |                                   ~~^~~
swap.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   17 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
swap.cpp:18:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |     for (int i = 1; i <= n; i++) scanf("%d", a + i);
      |                                  ~~~~~^~~~~~~~~~~~~