Submission #261613

# Submission time Handle Problem Language Result Execution time Memory
261613 2020-08-11T22:33:27 Z caoash Swap (BOI16_swap) C++14
21 / 100
1000 ms 504 KB
#include <bits/stdc++.h> 
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

using ll = long long;

using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

const int MX = 200005;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();

struct chash { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const
    { 
        auto hash1 = hash<T1>{}(p.first); 
        auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2 ^ RANDOM; 
    } 
};

int n; int num[MX]; gp_hash_table<pi, vi, chash> dp; 

vi merge(vi &a, int &x, vi &b) {
    vi ret;
    ret.pb(x);
    int p1 = 0, p2 = 0;
    for (int i = 0; i < 19; i++) {
        for (int j = 0; j < (1 << i); j++) {
            if (p1 < sz(a)) {
                ret.pb(a[p1++]);
            }
        } 
        for (int j = 0; j < (1 << i); j++) {
            if (p2 < sz(b)) {
                ret.pb(b[p2++]);
            } 
        }
    }
    return ret;
}

vi solve(int v, int c) {
    if (!dp[mp(v, c)].empty()) {
        // cout << "v: " << v << " " << c << endl;
        // pr(dp[mp(v, c)]);
        return dp[mp(v, c)];
    }
    int l = 2 * v + 1, r = 2 * v + 2;
    if (l >= n) {
        // cout << "v: " << v << " " << c << endl;
        // cout << c << '\n';
        return dp[mp(v, c)] = {c};
    }
    if (r >= n) {
        vi best; 
        vi fst = solve(l, num[l]);
        best.pb(c);
        for (int x : fst) best.pb(x);
        vi sec = solve(l, c);
        vi sbest;
        sbest.pb(num[l]);
        for (int x : sec) sbest.pb(x);
        return dp[mp(v, c)] = min(best, sbest);
    }
    // cout << "l, r" << " " << l << " " << r << '\n';
    vi best;
    // no swaps
    vi fst = solve(l, num[l]);
    vi scnd = solve(r, num[r]);
    best = merge(fst, c, scnd);

    // swap left
    if (l < n) {
        fst = solve(l, c), scnd = solve(r, num[r]);
        best = min(best, merge(fst, num[l], scnd));
    }

    // swap right
    if (r < n) {
        fst = solve(l, num[l]);
        scnd = solve(r, c);
        best = min(best, merge(fst, num[r], scnd));
    }

    // swap left, then right
    if (r < n) {
        fst = solve(l, c);
        scnd = solve(r, num[l]);
        best = min(best, merge(fst, num[r], scnd));
    }
    // cout << "v: " << v << " " << c << endl;
    // pr(best);
    return dp[mp(v, c)] = best;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> num[i];
    }
    solve(0, num[0]); 
    vi ans = dp[mp(0, num[0])];
    for (int i = 0; i < n; i++) {
        cout << ans[i] << ' ';
    }
    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 111 ms 384 KB Output is correct
2 Correct 100 ms 504 KB Output is correct
3 Correct 100 ms 408 KB Output is correct
4 Correct 110 ms 388 KB Output is correct
5 Correct 100 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 384 KB Output is correct
2 Correct 100 ms 504 KB Output is correct
3 Correct 100 ms 408 KB Output is correct
4 Correct 110 ms 388 KB Output is correct
5 Correct 100 ms 384 KB Output is correct
6 Correct 289 ms 504 KB Output is correct
7 Correct 292 ms 384 KB Output is correct
8 Correct 304 ms 432 KB Output is correct
9 Correct 296 ms 396 KB Output is correct
10 Correct 301 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 384 KB Output is correct
2 Correct 100 ms 504 KB Output is correct
3 Correct 100 ms 408 KB Output is correct
4 Correct 110 ms 388 KB Output is correct
5 Correct 100 ms 384 KB Output is correct
6 Correct 289 ms 504 KB Output is correct
7 Correct 292 ms 384 KB Output is correct
8 Correct 304 ms 432 KB Output is correct
9 Correct 296 ms 396 KB Output is correct
10 Correct 301 ms 504 KB Output is correct
11 Execution timed out 1084 ms 480 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 384 KB Output is correct
2 Correct 100 ms 504 KB Output is correct
3 Correct 100 ms 408 KB Output is correct
4 Correct 110 ms 388 KB Output is correct
5 Correct 100 ms 384 KB Output is correct
6 Correct 289 ms 504 KB Output is correct
7 Correct 292 ms 384 KB Output is correct
8 Correct 304 ms 432 KB Output is correct
9 Correct 296 ms 396 KB Output is correct
10 Correct 301 ms 504 KB Output is correct
11 Execution timed out 1084 ms 480 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 384 KB Output is correct
2 Correct 100 ms 504 KB Output is correct
3 Correct 100 ms 408 KB Output is correct
4 Correct 110 ms 388 KB Output is correct
5 Correct 100 ms 384 KB Output is correct
6 Correct 289 ms 504 KB Output is correct
7 Correct 292 ms 384 KB Output is correct
8 Correct 304 ms 432 KB Output is correct
9 Correct 296 ms 396 KB Output is correct
10 Correct 301 ms 504 KB Output is correct
11 Execution timed out 1084 ms 480 KB Time limit exceeded
12 Halted 0 ms 0 KB -