Submission #375336

# Submission time Handle Problem Language Result Execution time Memory
375336 2021-03-09T09:24:46 Z Mamnoon_Siam Swap (BOI16_swap) C++17
68 / 100
954 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second

const int N = 1 << 18 | 3;

unordered_map<int, vi> dp[N];
int a[N], n, nn;
vi todo[N];

vi merge(vi& p, vi& q, int x) {
  vi r; r.reserve(sz(p)+sz(q)+1);
  r.emplace_back(x);
  int pw = 1;
  int i = 0, j = 0;
  while(i < sz(p) or j < sz(q)) {
    r.insert(r.end(), p.begin()+i, p.begin()+min(sz(p), i+pw));
    r.insert(r.end(), q.begin()+j, q.begin()+min(sz(q), j+pw));
    i = min(i+pw, sz(p));
    j = min(j+pw, sz(q));
    pw *= 2;
  }
  return r;
}

int main(int argc, char* argv[]) {
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  scanf("%d", &n);
  nn = 1;
  while(nn-1 < n) nn *= 2;
  nn--;
  for(int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for(int i = n+1; i <= nn; ++i)
    a[i] = n+1;
  for(int i:{1,2,3})
    if(i <= nn) todo[1].emplace_back(a[i]);
  for(int i = 2; i <= nn; ++i) {
    for(int j:{i,2*i,2*i+1})
      if(j <= nn) todo[i].emplace_back(a[j]);
    int u = i;
    while(u != 1) {
      if(u & 1)
        todo[i].emplace_back(a[u^1]);
      todo[i].emplace_back(a[u >>= 1]);
    }
  }
  for(int i = nn/2+1; i <= nn; ++i) {
    for(int x : todo[i])
      dp[i][x] = {x};
  }
  for(int u = nn/2; u >= 1; --u) {
    for(int k : todo[u]) {
      vi& ret = dp[u][k];
      { // don't use any edge
        vi& l = dp[2*u][a[2*u]];
        vi& r = dp[2*u+1][a[2*u+1]];
        ret = merge(l, r, k);
      }
      { // use only left edge
        vi& l = dp[2*u][k];
        vi& r = dp[2*u+1][a[2*u+1]];
        vi tmp = merge(l, r, a[2*u]);
        ret = min(ret, tmp);
      }
      { // use only right edge
        vi& l = dp[2*u][a[2*u]];
        vi& r = dp[2*u+1][k];
        vi tmp = merge(l, r, a[2*u+1]);
        ret = min(ret, tmp);
      }
      { // use both edges
        vi& l = dp[2*u][k];
        vi& r = dp[2*u+1][a[2*u]];
        vi tmp = merge(l, r, a[2*u+1]);
        ret = min(ret, tmp);
      }
    }
    for(int v:{2*u,2*u+1}) {
      // for(auto pa : dp[v]) pa.second.clear();
      dp[v].clear();
    }
  }
  vi ans = dp[1][a[1]];
  for(int i = 0; i < n; ++i)
    cout << ans[i] << ' ';
  cout << endl;
}

Compilation message

swap.cpp: In function 'int main(int, char**)':
swap.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
swap.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   42 |     scanf("%d", &a[i]);
      |     ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20844 KB Output is correct
2 Correct 17 ms 20844 KB Output is correct
3 Correct 18 ms 20844 KB Output is correct
4 Correct 15 ms 20844 KB Output is correct
5 Correct 15 ms 20844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20844 KB Output is correct
2 Correct 17 ms 20844 KB Output is correct
3 Correct 18 ms 20844 KB Output is correct
4 Correct 15 ms 20844 KB Output is correct
5 Correct 15 ms 20844 KB Output is correct
6 Correct 15 ms 20844 KB Output is correct
7 Correct 15 ms 20844 KB Output is correct
8 Correct 17 ms 20844 KB Output is correct
9 Correct 15 ms 20844 KB Output is correct
10 Correct 15 ms 20844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20844 KB Output is correct
2 Correct 17 ms 20844 KB Output is correct
3 Correct 18 ms 20844 KB Output is correct
4 Correct 15 ms 20844 KB Output is correct
5 Correct 15 ms 20844 KB Output is correct
6 Correct 15 ms 20844 KB Output is correct
7 Correct 15 ms 20844 KB Output is correct
8 Correct 17 ms 20844 KB Output is correct
9 Correct 15 ms 20844 KB Output is correct
10 Correct 15 ms 20844 KB Output is correct
11 Correct 22 ms 21740 KB Output is correct
12 Correct 23 ms 21740 KB Output is correct
13 Correct 22 ms 21740 KB Output is correct
14 Correct 22 ms 21740 KB Output is correct
15 Correct 22 ms 21740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20844 KB Output is correct
2 Correct 17 ms 20844 KB Output is correct
3 Correct 18 ms 20844 KB Output is correct
4 Correct 15 ms 20844 KB Output is correct
5 Correct 15 ms 20844 KB Output is correct
6 Correct 15 ms 20844 KB Output is correct
7 Correct 15 ms 20844 KB Output is correct
8 Correct 17 ms 20844 KB Output is correct
9 Correct 15 ms 20844 KB Output is correct
10 Correct 15 ms 20844 KB Output is correct
11 Correct 22 ms 21740 KB Output is correct
12 Correct 23 ms 21740 KB Output is correct
13 Correct 22 ms 21740 KB Output is correct
14 Correct 22 ms 21740 KB Output is correct
15 Correct 22 ms 21740 KB Output is correct
16 Correct 948 ms 102304 KB Output is correct
17 Correct 954 ms 102308 KB Output is correct
18 Correct 931 ms 102380 KB Output is correct
19 Correct 920 ms 102200 KB Output is correct
20 Correct 904 ms 102308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20844 KB Output is correct
2 Correct 17 ms 20844 KB Output is correct
3 Correct 18 ms 20844 KB Output is correct
4 Correct 15 ms 20844 KB Output is correct
5 Correct 15 ms 20844 KB Output is correct
6 Correct 15 ms 20844 KB Output is correct
7 Correct 15 ms 20844 KB Output is correct
8 Correct 17 ms 20844 KB Output is correct
9 Correct 15 ms 20844 KB Output is correct
10 Correct 15 ms 20844 KB Output is correct
11 Correct 22 ms 21740 KB Output is correct
12 Correct 23 ms 21740 KB Output is correct
13 Correct 22 ms 21740 KB Output is correct
14 Correct 22 ms 21740 KB Output is correct
15 Correct 22 ms 21740 KB Output is correct
16 Correct 948 ms 102304 KB Output is correct
17 Correct 954 ms 102308 KB Output is correct
18 Correct 931 ms 102380 KB Output is correct
19 Correct 920 ms 102200 KB Output is correct
20 Correct 904 ms 102308 KB Output is correct
21 Runtime error 556 ms 262148 KB Execution killed with signal 9
22 Halted 0 ms 0 KB -