Submission #240310

#TimeUsernameProblemLanguageResultExecution timeMemory
240310karmaSwap (BOI16_swap)C++14
100 / 100
62 ms7288 KiB
#include <bits/stdc++.h>
#define pb          emplace_back
#define ll          long long
#define fi          first
#define se          second
#define mp          make_pair
//#define int         int64_t

using namespace std;

const int N = int(5e5) + 7;
const int logN = 18;
typedef pair<int, int> pii;

int n, a[N], b[N], can[N];

int get(int x) {
    for(int res = 1e9; ; x >>= 1) {
        if(can[x] != 1) res = min(res, a[x]);
        if(can[x] == 0) return res;
        if((x & 1) && can[x ^ 1]) {
            res = min(res, a[x ^ 1]);
            if(can[x ^ 1] == 1) return res;
        }
    }
    return 1e9;
}

void change(int x, int v) {
    for(;; x >>= 1) {
        if(a[x] == v) {
            can[x] = 0;
            return;
        }
        if(x & 1) {
            if(a[x ^ 1] == v) {
                can[x] = can[x ^ 1] = 1;
                return;
            } else can[x ^ 1] = 0;
        }
        can[x] = 1;
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define Task        "test"
    if(fopen(Task".inp", "r")) {
        freopen(Task".inp", "r", stdin);
        freopen(Task".out", "w", stdout);
    }
    cin >> n; fill_n(a, N, 1e9);
    for(int i = 1; i <= n; ++i) cin >> a[i], can[i] = i == 1? 0: 2;
    for(int x, i = 1; i <= n; ++i) {
        b[i] = min({x = get(i), a[i << 1], a[i << 1 | 1]});
        if(b[i] == x) {
           change(i, x);
           can[i << 1] = can[i << 1 | 1] = 0;
        } else if(b[i] == a[i << 1]) can[i << 1] = 1, can[i << 1 | 1] = 0;
        else can[i << 1 | 1] = 1;
    }
    for(int i = 1; i <= n; ++i) cout << b[i] << ' ';
}

Compilation message (stderr)

swap.cpp: In function 'int32_t main()':
swap.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(Task".inp", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(Task".out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...