Submission #759336

# Submission time Handle Problem Language Result Execution time Memory
759336 2023-06-16T07:49:53 Z fanwen Swap (BOI16_swap) C++17
100 / 100
567 ms 3648 KB
/**
 *      author : pham van sam 
 *      created : 16 June 2023 (Friday)
 **/

#include <bits/stdc++.h>

using namespace std;
using namespace chrono;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)

template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }

const int MAXN = 2e5 + 5;

int N, a[MAXN];

int dp(int u, int val) {    
    if(u * 2 > N) return u;
    if(val < min(a[u * 2], a[u * 2 + 1])) return u;
    if(a[u * 2] < min(val, a[u * 2 + 1])) return dp(u * 2, val);
    int Min = min(a[u * 2], val);
    if(dp(u * 2, Min) < dp(u * 2 + 1, Min)) return (val == Min ? dp(u * 2, val) : dp(u * 2 + 1, val));
    return (val == Min ? dp(u * 2 + 1, val) : dp(u * 2, val));
}

void process(void) {
    cin >> N;
    memset(a, 0x3f, sizeof a);
    FOR(i, 1, N) cin >> a[i];

    FOR(u, 1, N) {
        if(u * 2 > N) continue;
        if(a[u] < min(a[u * 2], a[u * 2 + 1])) continue;
        if(a[u * 2] < min(a[u], a[u * 2 + 1])) {
            swap(a[u * 2], a[u]);
            continue;
        }
        swap(a[u], a[u * 2 + 1]);
        int Min = min(a[u * 2], a[u * 2 + 1]);
        if(dp(u * 2, Min) < dp(u * 2 + 1, Min)) {
            if(a[u * 2] > a[u * 2 + 1]) swap(a[u * 2], a[u * 2 + 1]);
        } else if(a[u * 2] < a[u * 2 + 1]) swap(a[u * 2], a[u * 2 + 1]);
    }
    FOR(i, 1, N) cout << a[i] << " \n"[i == N];
}

signed main() {

    #define TASK "TASK"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    auto start_time = steady_clock::now();
    int test = 1;
    // cin >> test;
    for (int i = 1; i <= test; ++i) {
        process();
        // cout << '\n';
    }

    auto end_time = steady_clock::now();

    cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1096 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1096 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1096 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 7 ms 1620 KB Output is correct
17 Correct 7 ms 1620 KB Output is correct
18 Correct 7 ms 1636 KB Output is correct
19 Correct 64 ms 1644 KB Output is correct
20 Correct 63 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1096 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 7 ms 1620 KB Output is correct
17 Correct 7 ms 1620 KB Output is correct
18 Correct 7 ms 1636 KB Output is correct
19 Correct 64 ms 1644 KB Output is correct
20 Correct 63 ms 1612 KB Output is correct
21 Correct 29 ms 3604 KB Output is correct
22 Correct 29 ms 3576 KB Output is correct
23 Correct 28 ms 3532 KB Output is correct
24 Correct 545 ms 3648 KB Output is correct
25 Correct 567 ms 3632 KB Output is correct