답안 #868750

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868750 2023-11-01T22:50:18 Z hazzle Swap (BOI16_swap) C++17
68 / 100
458 ms 262144 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define all(m) (m).begin(), (m).end()
#define rall(m) (m).rbegin(), (m).rend()
#define vec vector
#define sz(a) (int) (a).size()
#define mpp make_pair
#define mt t make_tuple

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef tuple <int, int, int> tui;

template <typename T>
using prq = priority_queue <T>;

template <typename T>
using pgq = priority_queue <T, vec <T>, greater <T>>;

template <typename T>
using oset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key

template <typename T> bool umin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> bool umax(T &a, T b) { return a < b ? a = b, 1 : 0; }

inline int solve(){
      int n; cin >> n;
      vec <int> a(n + 1);
      for (int i = 1; i <= n; ++i) cin >> a[i];
      auto mrg = [&](int x, vec <int> lf, vec <int> rt){
            vec <int> res(1 + sz(lf) + sz(rt));
            res[0] = x;
            int p1 = 0, p2 = 0;
            int st = 1, pt = 1;
            while(pt < sz(res)){
                  for (int i = 0; i < st && p1 < sz(lf) && pt < sz(res); ++i){
                        res[pt++] = lf[p1++];
                  }
                  for (int i = 0; i < st && p2 < sz(rt) && pt < sz(res); ++i){
                        res[pt++] = rt[p2++];
                  }
                  st <<= 1;
            }
            return res;
      };
      vec <set <int>> val(n + 1);
      auto ini = [&](auto &&ini, int i, int ai) -> void{
            if (val[i].count(ai)) return;
            val[i].insert(ai);
            int ls = 2 * i, rs = 2 * i + 1;
            if (ls > n) return;
            if (rs > n) return;
            if (ai < min(a[ls], a[rs])){
                  ini(ini, ls, a[ls]), ini(ini, rs, a[rs]);
            }
            else if (a[ls] < a[rs]){
                  ini(ini, ls, ai), ini(ini, rs, a[rs]);
            }
            else{
                  ini(ini, ls, a[ls]), ini(ini, rs, ai);
                  ini(ini, ls, ai), ini(ini, rs, a[ls]);
            }
      };
      ini(ini, 1, a[1]);
      vec <map <int, vec <int>>> dp(n + 1);
      for (int i = n; i > 0; --i){
            int ls = 2 * i, rs = 2 * i + 1;
            for (auto &ai: val[i]){
                  if (ls > n){
                        dp[i][ai] = {ai};
                        continue;
                  }
                  if (rs > n){
                        dp[i][ai] = {min(ai, a[ls]), max(ai, a[ls])};
                        continue;
                  }
                  if (ai < min(a[ls], a[rs])){
                        dp[i][ai] = mrg(ai, dp[ls][a[ls]], dp[rs][a[rs]]);
                  }
                  else if (a[ls] < a[rs]){
                        dp[i][ai] = mrg(a[ls], dp[ls][ai], dp[rs][a[rs]]);
                  }
                  else dp[i][ai] = min(mrg(a[rs], dp[ls][a[ls]], dp[rs][ai]),
                                       mrg(a[rs], dp[ls][ai], dp[rs][a[ls]]));
            }
           	if (ls <= n) dp[ls].clear();
        	if (rs <= n) dp[rs].clear();
      }
      vec <int> res = dp[1][a[1]];
      for (auto &i: res) cout << i << " ";
      return 0;
}

inline void precalc(){}

signed main(){
      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
      int tst = 1; //cin >> tst;
      precalc();
      while(tst--) solve();
      return 0;
}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 0 ms 348 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 344 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 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 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 3 ms 1372 KB Output is correct
15 Correct 3 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 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 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 3 ms 1372 KB Output is correct
15 Correct 3 ms 1372 KB Output is correct
16 Correct 43 ms 17748 KB Output is correct
17 Correct 45 ms 17748 KB Output is correct
18 Correct 53 ms 18004 KB Output is correct
19 Correct 336 ms 80856 KB Output is correct
20 Correct 380 ms 80188 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 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 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 3 ms 1372 KB Output is correct
15 Correct 3 ms 1372 KB Output is correct
16 Correct 43 ms 17748 KB Output is correct
17 Correct 45 ms 17748 KB Output is correct
18 Correct 53 ms 18004 KB Output is correct
19 Correct 336 ms 80856 KB Output is correct
20 Correct 380 ms 80188 KB Output is correct
21 Correct 246 ms 70736 KB Output is correct
22 Correct 241 ms 70736 KB Output is correct
23 Correct 242 ms 70736 KB Output is correct
24 Runtime error 458 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -