답안 #998840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998840 2024-06-14T17:33:29 Z underwaterkillerwhale Swap (BOI16_swap) C++17
68 / 100
537 ms 262144 KB
#pragma GCC optimize("conserve-stack")

#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const int N  = 2e5 + 7;
const int Mod = 998244353;
const int szBL = 916;
const int INF = 1e9;
const int BASE = 137;

int n;
int a[N];
unordered_map<int, vector<int>> dp[N];

vector<int> Merge (int A, vector<int> &lc, vector<int> &rc) {
    int ptr1 = 0, ptr2 = 0;
    static vector<int> vec;
    vec = {A};
    rep (i, 0, 20) {
        if ((1 << i) > SZ(lc) - ptr1) break;
        rep (j, 1, (1 << i)) vec.push_back(lc[ptr1++]);
        rep (j, 1, (1 << i)) {
            if (ptr2 < SZ(rc)) vec.push_back(rc[ptr2++]);
        }
    }
    while (ptr1 < SZ(lc)) vec.push_back(lc[ptr1++]);
    return vec;
}

bool compare (vector<int> &v1, vector<int> &v2) {
    int pos = 0;
    assert (SZ(v1) == SZ(v2));
    while (v1[pos] == v2[pos]) ++pos;
    return v1[pos] < v2[pos];
}

void calc (int id, int val);
void Assign (int root, int valr) {
    int lc = root * 2, rc = root * 2 + 1;
    if (valr < a[lc] && valr < a[rc]) {
        calc (lc, a[lc]), calc (rc, a[rc]);
        dp[root][valr] = Merge (valr, dp[lc][a[lc]], dp[rc][a[rc]]);
    }
    else if (a[lc] < min (valr, a[rc])) {
        calc (lc, valr), calc (rc, a[rc]);
        dp[root][valr] = Merge (a[lc], dp[lc][valr], dp[rc][a[rc]]);
    }
    else {
        int B = min (valr, a[lc]);
        int C = max (valr, a[lc]);
        int A = a[rc];
        calc (lc, B);
        calc (rc, C);
        calc (lc, C);
        calc (rc, B);
        vector<int> t1 = Merge (A, dp[lc][B], dp[rc][C]), t2 = Merge (A, dp[lc][C], dp[rc][B]);
        if (compare (t1, t2)) dp[root][valr] = t1;
        else dp[root][valr] = t2;
    }
}

void calc (int id, int val) {
    if (id > n) {
        vector<int> (0);
        return;
    }
    if (id * 2 > n) {
        dp[id][val] = vector<int> (1, val);
        return;
    }
    if (id * 2 + 1 > n) {
        dp[id][val] = {min(val, a[id * 2]), max(val, a[id * 2])};
        return;
    }
    if (dp[id].find(val) != dp[id].end())
        return;
    Assign (id, val);
    return;
}

void solution () {
    cin >> n;
    rep (i, 1, n) cin >> a[i];
    if (n <= 2) {
        sort (a + 1, a + 1 + n);
        cout << a[1] <<" "<<a[2];
        return;
    }
    vector<int> res;
    Assign (1, a[1]);
    iter (&id, dp[1][a[1]]) cout << id <<" ";
}

#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    file ("c");
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
5 10
1 2
3 1
4 2
5 3
1 4
2 3
3 5
4 5
3 4
1 4

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11356 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11356 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11380 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11356 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11380 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 5 ms 11612 KB Output is correct
12 Correct 4 ms 11612 KB Output is correct
13 Correct 4 ms 11612 KB Output is correct
14 Correct 4 ms 12124 KB Output is correct
15 Correct 5 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11356 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11380 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 5 ms 11612 KB Output is correct
12 Correct 4 ms 11612 KB Output is correct
13 Correct 4 ms 11612 KB Output is correct
14 Correct 4 ms 12124 KB Output is correct
15 Correct 5 ms 12124 KB Output is correct
16 Correct 52 ms 31700 KB Output is correct
17 Correct 43 ms 31624 KB Output is correct
18 Correct 41 ms 31700 KB Output is correct
19 Correct 308 ms 96212 KB Output is correct
20 Correct 306 ms 95428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11356 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11380 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 5 ms 11612 KB Output is correct
12 Correct 4 ms 11612 KB Output is correct
13 Correct 4 ms 11612 KB Output is correct
14 Correct 4 ms 12124 KB Output is correct
15 Correct 5 ms 12124 KB Output is correct
16 Correct 52 ms 31700 KB Output is correct
17 Correct 43 ms 31624 KB Output is correct
18 Correct 41 ms 31700 KB Output is correct
19 Correct 308 ms 96212 KB Output is correct
20 Correct 306 ms 95428 KB Output is correct
21 Correct 238 ms 96840 KB Output is correct
22 Correct 240 ms 96544 KB Output is correct
23 Correct 259 ms 97204 KB Output is correct
24 Runtime error 537 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -