Submission #873392

# Submission time Handle Problem Language Result Execution time Memory
873392 2023-11-15T02:23:17 Z anha3k25cvp Swap (BOI16_swap) C++14
68 / 100
359 ms 262144 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

vector <vector <vector <int>>> g;

void Merge(vector <int> &a, vector <int> &b, vector <int> &c) {
    for (int i = 0, j = 0, l = 1; i < b.size() || j < c.size(); i += l, j += l, l *= 2) {
        for (int pos = 0; pos < l && i + pos < b.size(); pos ++)
            a.push_back(b[i + pos]);
        for (int pos = 0; pos < l && j + pos < c.size(); pos ++)
            a.push_back(c[j + pos]);
    }
}

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    int n;
    cin >> n;
    vector <int> a(n + 1, 0);
    for (int i = 1; i <= n; i ++)
        cin >> a[i];
    int logn;
    for (logn = 1; (1 << logn) <= n; logn ++);
    logn *= 2;
    vector <vector <int>> f(n + 1, vector <int> (logn, 0));
    f[1][0] = 1;
    for (int i = 1; i * 2 + 1 <= n; i ++)
        for (int j = 0; j < logn; j ++)
            if (f[i][j]) {
                int pos = i >> j / 2 ^ j & 1;
                if (a[2 * i + 1] < a[pos] && a[2 * i + 1] < a[2 * i])
                    f[2 * i][j + 2] = f[2 * i][0] = f[2 * i + 1][j + 2] = f[2 * i + 1][1] = 1;
                else if (a[2 * i] < a[pos])
                    f[2 * i][j + 2] = f[2 * i + 1][0] = 1;
                else
                    f[2 * i][0] = f[2 * i + 1][0] = 1;
            }
    g.assign(n + 1, vector <vector <int>> (logn));
    for (int i = n; i > 0; i --) {
        for (int j = 0; j < logn; j ++)
            if (f[i][j]) {
                int pos = i >> j / 2 ^ j & 1;
                if (2 * i + 1 <= n) {
                    if (a[2 * i + 1] < a[pos] && a[2 * i + 1] < a[2 * i]) {
                        vector <int> v1(1, a[2 * i + 1]), v2(1, a[2 * i + 1]);
                        Merge(v1, g[2 * i][j + 2], g[2 * i + 1][1]);
                        Merge(v2, g[2 * i][0], g[2 * i + 1][j + 2]);
                        g[i][j] = min(v1, v2);
                    }
                    else if (a[2 * i] < a[pos]) {
                        g[i][j].push_back(a[2 * i]);
                        Merge(g[i][j], g[2 * i][j + 2], g[2 * i + 1][0]);
                    }
                    else {
                        g[i][j].push_back(a[pos]);
                        Merge(g[i][j], g[2 * i][0], g[2 * i + 1][0]);
                    }
                }
                else if (i * 2 <= n)
                    g[i][j] = {min(a[pos], a[2 * i]), max(a[pos], a[2 * i])};
                else
                    g[i][j] = {a[pos]};
            }
        if (2 * i + 1 <= n)
            for (int val = 0; val < 2; val ++)
                for (int j = 0; j < logn; j ++)
                    if (f[2 * i + val][j])
                        g[2 * i + val][j].clear();
    }
    for (int u : g[1][0])
        cout << u << ' ';
    return 0;
}

Compilation message

swap.cpp: In function 'void Merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:17:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for (int i = 0, j = 0, l = 1; i < b.size() || j < c.size(); i += l, j += l, l *= 2) {
      |                                   ~~^~~~~~~~~~
swap.cpp:17:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for (int i = 0, j = 0, l = 1; i < b.size() || j < c.size(); i += l, j += l, l *= 2) {
      |                                                   ~~^~~~~~~~~~
swap.cpp:18:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         for (int pos = 0; pos < l && i + pos < b.size(); pos ++)
      |                                      ~~~~~~~~^~~~~~~~~~
swap.cpp:20:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |         for (int pos = 0; pos < l && j + pos < c.size(); pos ++)
      |                                      ~~~~~~~~^~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:46:42: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   46 |                 int pos = i >> j / 2 ^ j & 1;
      |                                        ~~^~~
swap.cpp:58:42: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   58 |                 int pos = i >> j / 2 ^ j & 1;
      |                                        ~~^~~
swap.cpp:30:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen (TASKNAME".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:31:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen (TASKNAME".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 1112 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 1 ms 968 KB Output is correct
14 Correct 2 ms 1220 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 1112 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 1 ms 968 KB Output is correct
14 Correct 2 ms 1220 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 80 ms 57320 KB Output is correct
17 Correct 74 ms 56984 KB Output is correct
18 Correct 69 ms 57356 KB Output is correct
19 Correct 182 ms 88496 KB Output is correct
20 Correct 187 ms 87980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 1112 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 1 ms 968 KB Output is correct
14 Correct 2 ms 1220 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 80 ms 57320 KB Output is correct
17 Correct 74 ms 56984 KB Output is correct
18 Correct 69 ms 57356 KB Output is correct
19 Correct 182 ms 88496 KB Output is correct
20 Correct 187 ms 87980 KB Output is correct
21 Correct 359 ms 253920 KB Output is correct
22 Correct 328 ms 254500 KB Output is correct
23 Correct 318 ms 254760 KB Output is correct
24 Runtime error 212 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -