Submission #322251

# Submission time Handle Problem Language Result Execution time Memory
322251 2020-11-14T10:31:01 Z dolphingarlic Swap (BOI16_swap) C++14
68 / 100
937 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;

int a[200002];
bool needed[200002][18][2];
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);

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

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

            if (node * 2 > n) dp[node][i][j] = {a[((node >> i + 1) << 1) + j]};
            else if (node * 2 == n) {
                if (a[((node >> i + 1) << 1) + j] < a[2 * node]) dp[node][i][j] = {a[((node >> i + 1) << 1) + j], a[2 * node]};
                else dp[node][i][j] = {a[2 * node], a[((node >> i + 1) << 1) + j]};
            } else {
                int mn = min({a[((node >> i + 1) << 1) + j], a[2 * node], a[2 * node + 1]});
                if (mn == a[((node >> i + 1) << 1) + j]) {
                    dp[node][i][j] = {a[((node >> i + 1) << 1) + j]};
                    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);
                }
            }
        }
        if (node * 2 <= n) {
            for (int i = 0; node * 2 >> i; i++) for (bool j : {0, 1}) {
                vector<int>().swap(dp[node * 2][i][j]);
                vector<int>().swap(dp[node * 2 + 1][i][j]);
            }
        }
    }

    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:23:48: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |             if (node * 2 >= n || !(((node >> i + 1) << 1) + j)) continue;
      |                                              ~~^~~
swap.cpp:24:41: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |             int mn = min({a[((node >> i + 1) << 1) + j], a[2 * node], a[2 * node + 1]});
      |                                       ~~^~~
swap.cpp:25:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |             if (mn == a[((node >> i + 1) << 1) + j]) {
      |                                   ~~^~~
swap.cpp:40:63: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |             if (node * 2 > n) dp[node][i][j] = {a[((node >> i + 1) << 1) + j]};
      |                                                             ~~^~~
swap.cpp:42:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |                 if (a[((node >> i + 1) << 1) + j] < a[2 * node]) dp[node][i][j] = {a[((node >> i + 1) << 1) + j], a[2 * node]};
      |                                 ~~^~~
swap.cpp:42:98: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |                 if (a[((node >> i + 1) << 1) + j] < a[2 * node]) dp[node][i][j] = {a[((node >> i + 1) << 1) + j], a[2 * node]};
      |                                                                                                ~~^~~
swap.cpp:43:67: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |                 else dp[node][i][j] = {a[2 * node], a[((node >> i + 1) << 1) + j]};
      |                                                                 ~~^~~
swap.cpp:45:45: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |                 int mn = min({a[((node >> i + 1) << 1) + j], a[2 * node], a[2 * node + 1]});
      |                                           ~~^~~
swap.cpp:46:41: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |                 if (mn == a[((node >> i + 1) << 1) + j]) {
      |                                       ~~^~~
swap.cpp:47:53: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |                     dp[node][i][j] = {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);
      |                                  ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 98 ms 169452 KB Output is correct
2 Correct 99 ms 169452 KB Output is correct
3 Correct 99 ms 169452 KB Output is correct
4 Correct 99 ms 169452 KB Output is correct
5 Correct 100 ms 169452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 169452 KB Output is correct
2 Correct 99 ms 169452 KB Output is correct
3 Correct 99 ms 169452 KB Output is correct
4 Correct 99 ms 169452 KB Output is correct
5 Correct 100 ms 169452 KB Output is correct
6 Correct 98 ms 169444 KB Output is correct
7 Correct 100 ms 169452 KB Output is correct
8 Correct 98 ms 169452 KB Output is correct
9 Correct 98 ms 169452 KB Output is correct
10 Correct 98 ms 169476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 169452 KB Output is correct
2 Correct 99 ms 169452 KB Output is correct
3 Correct 99 ms 169452 KB Output is correct
4 Correct 99 ms 169452 KB Output is correct
5 Correct 100 ms 169452 KB Output is correct
6 Correct 98 ms 169444 KB Output is correct
7 Correct 100 ms 169452 KB Output is correct
8 Correct 98 ms 169452 KB Output is correct
9 Correct 98 ms 169452 KB Output is correct
10 Correct 98 ms 169476 KB Output is correct
11 Correct 99 ms 169580 KB Output is correct
12 Correct 101 ms 169580 KB Output is correct
13 Correct 102 ms 169580 KB Output is correct
14 Correct 101 ms 169836 KB Output is correct
15 Correct 101 ms 169708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 169452 KB Output is correct
2 Correct 99 ms 169452 KB Output is correct
3 Correct 99 ms 169452 KB Output is correct
4 Correct 99 ms 169452 KB Output is correct
5 Correct 100 ms 169452 KB Output is correct
6 Correct 98 ms 169444 KB Output is correct
7 Correct 100 ms 169452 KB Output is correct
8 Correct 98 ms 169452 KB Output is correct
9 Correct 98 ms 169452 KB Output is correct
10 Correct 98 ms 169476 KB Output is correct
11 Correct 99 ms 169580 KB Output is correct
12 Correct 101 ms 169580 KB Output is correct
13 Correct 102 ms 169580 KB Output is correct
14 Correct 101 ms 169836 KB Output is correct
15 Correct 101 ms 169708 KB Output is correct
16 Correct 266 ms 183536 KB Output is correct
17 Correct 258 ms 182892 KB Output is correct
18 Correct 279 ms 184512 KB Output is correct
19 Correct 412 ms 194796 KB Output is correct
20 Correct 411 ms 194540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 169452 KB Output is correct
2 Correct 99 ms 169452 KB Output is correct
3 Correct 99 ms 169452 KB Output is correct
4 Correct 99 ms 169452 KB Output is correct
5 Correct 100 ms 169452 KB Output is correct
6 Correct 98 ms 169444 KB Output is correct
7 Correct 100 ms 169452 KB Output is correct
8 Correct 98 ms 169452 KB Output is correct
9 Correct 98 ms 169452 KB Output is correct
10 Correct 98 ms 169476 KB Output is correct
11 Correct 99 ms 169580 KB Output is correct
12 Correct 101 ms 169580 KB Output is correct
13 Correct 102 ms 169580 KB Output is correct
14 Correct 101 ms 169836 KB Output is correct
15 Correct 101 ms 169708 KB Output is correct
16 Correct 266 ms 183536 KB Output is correct
17 Correct 258 ms 182892 KB Output is correct
18 Correct 279 ms 184512 KB Output is correct
19 Correct 412 ms 194796 KB Output is correct
20 Correct 411 ms 194540 KB Output is correct
21 Correct 899 ms 233324 KB Output is correct
22 Correct 937 ms 235244 KB Output is correct
23 Correct 910 ms 232956 KB Output is correct
24 Runtime error 306 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
25 Halted 0 ms 0 KB -