Submission #827886

# Submission time Handle Problem Language Result Execution time Memory
827886 2023-08-16T21:31:15 Z ikaurov Ancient Machine 2 (JOI23_ancient2) C++17
37 / 100
55 ms 476 KB
#include "ancient2.h"
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'

vector<int> nums = {4, 5, 7, 9};
bool first1;

int query(std::vector<int> a, std::vector<int> b){
  if (first1) swap(a, b);
  return Query(sz(a), a, b);
}

int ask(int idx, vector<vector<int>> a, bool type){
  if (idx == -1){
    int to_add = sz(nums);
    for (int i = 0; i < to_add; i++){
      a[type].pb(sz(a[type]) + 1);
      a[type ^ 1].pb(sz(a[type ^ 1]) + 1 + to_add);
    }
    for (int i = 0; i < to_add + 1; i++) a[0].pb(sz(a[0])), a[1].pb(sz(a[1]));
    int node = query(a[0], a[1]);
    int midnode = sz(a[0]) - to_add - 1;
    if (node == midnode) return 0;
    else if (node < midnode) return node - (midnode - to_add) + 1;
    else return node - midnode;
  }
  int to_add = nums[idx];
  for (int i = 0; i < to_add; i++) a[type].pb(sz(a[type]) + 1), a[type ^ 1].pb(sz(a[type ^ 1]) + to_add);
  a[type].back() = sz(a[type]) - to_add;
  for (int i = 0; i < to_add; i++) a[0].pb(sz(a[0])), a[1].pb(sz(a[1]));
  int node = query(a[0], a[1]);
  return (node - (sz(a[0]) - to_add * 2) + 1) % to_add;
}

int reconstruct(vector<int> mods){
  for (int i = 0;; i++){
    bool good = 1;
    for (int j = 0; j < sz(nums); j++){
      good &= (i % nums[j] == mods[j]);
    }
    if (good) return i;
  }
}

vector<vector<int>> compress(vector<int>& dp, vector<pair<int, int>>& par, vector<int> blocks){
  blocks.pb(1);
  vector<bool> s;
  for (int i = 0; i < sz(blocks); i++){
    while (blocks[i]--){
      s.pb(i & 1);
    }
  }
  int n = sz(s);

  auto get = [&] (int idx){
    return s[idx - 1];
  };

  for (int i = sz(dp); i <= n; i++){
    int cur = get(i);
    dp.pb(dp.back() + 1);
    par.pb({0, 0});
    for (int l = 1; l <= n; l++){
      for (int c = 1; c * l < i; c++){
        if (get(i - c * l) == cur) break;
        int curdp = dp[i - c * l - 1] + l;
        if (curdp < dp[i]) dp[i] = curdp, par[i] = {l, c};
      }
    }
  }

  vector<pair<int, bool>> cycles;
  for (int i = n; i > 0;){
    cycles.pb({par[i].fi, get(i)});
    i -= par[i].fi * par[i].se + 1;
  }
  reverse(all(cycles));
  vector<vector<int>> a(2);
  for (auto [l, type] : cycles){
    if (l == 0){
      a[0].pb(sz(a[0]) + 1);
      a[1].pb(sz(a[1]) + 1);
      continue;
    }
    a[type].pb(sz(a[type]) + l);
    a[type ^ 1].pb(sz(a[type ^ 1]) + (l > 1));
    if (l == 1) continue;
    for (int i = 1; i < l; i++) a[0].pb(sz(a[0]) + 1), a[1].pb(sz(a[1]) + 1);
    a[0].back() -= l, a[1].back() -= l;
  }
  return a;
}

string Solve(int n){
  first1 = (Query(3, {1, 1, 2}, {2, 1, 2}) == 2);
  vector<int> blocks, dp{0};
  vector<pair<int, int>> par{pair{0, 0}};
  while (accumulate(all(blocks), 0) < n - 1){
    auto a = compress(dp, par, blocks);
    vector<int> mods(sz(nums));
    int val = ask(-1, a, sz(blocks) & 1);
    if (val){
      blocks.pb(val);
      continue;
    }
    for (int i = 0; i < sz(mods); i++) mods[i] = ask(i, a, sz(blocks) & 1);
    blocks.pb(reconstruct(mods));
  }
  if (accumulate(all(blocks), 0) == n - 1) blocks.pb(1);
  string ans;
  for (int i = 0; i < sz(blocks); i++){
    ans += string(blocks[i], '0' + ((i & 1) ^ first1));
  }
  return ans;
}
# Verdict Execution time Memory Grader output
1 Partially correct 12 ms 360 KB Output is partially correct
2 Correct 11 ms 372 KB Output is correct
3 Partially correct 8 ms 376 KB Output is partially correct
4 Correct 8 ms 364 KB Output is correct
5 Partially correct 11 ms 360 KB Output is partially correct
6 Correct 5 ms 356 KB Output is correct
7 Partially correct 14 ms 432 KB Output is partially correct
8 Partially correct 36 ms 440 KB Output is partially correct
9 Partially correct 17 ms 464 KB Output is partially correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 6 ms 300 KB Output is correct
13 Correct 5 ms 312 KB Output is correct
14 Correct 7 ms 336 KB Output is correct
15 Correct 4 ms 332 KB Output is correct
16 Partially correct 55 ms 460 KB Output is partially correct
17 Partially correct 55 ms 476 KB Output is partially correct
18 Correct 12 ms 376 KB Output is correct
19 Correct 14 ms 336 KB Output is correct
20 Partially correct 26 ms 388 KB Output is partially correct
21 Correct 12 ms 360 KB Output is correct
22 Correct 16 ms 324 KB Output is correct
23 Correct 12 ms 336 KB Output is correct
24 Correct 12 ms 364 KB Output is correct
25 Correct 10 ms 328 KB Output is correct
26 Correct 15 ms 364 KB Output is correct
27 Correct 11 ms 360 KB Output is correct
28 Correct 11 ms 364 KB Output is correct
29 Correct 10 ms 336 KB Output is correct
30 Partially correct 20 ms 360 KB Output is partially correct
31 Partially correct 8 ms 368 KB Output is partially correct
32 Correct 6 ms 328 KB Output is correct
33 Correct 5 ms 304 KB Output is correct
34 Correct 5 ms 296 KB Output is correct
35 Correct 3 ms 320 KB Output is correct
36 Correct 3 ms 316 KB Output is correct
37 Correct 2 ms 208 KB Output is correct
38 Partially correct 13 ms 404 KB Output is partially correct
39 Partially correct 16 ms 356 KB Output is partially correct
40 Correct 19 ms 336 KB Output is correct
41 Partially correct 19 ms 364 KB Output is partially correct
42 Partially correct 15 ms 352 KB Output is partially correct
43 Partially correct 15 ms 336 KB Output is partially correct
44 Partially correct 17 ms 360 KB Output is partially correct
45 Partially correct 13 ms 384 KB Output is partially correct
46 Partially correct 14 ms 356 KB Output is partially correct
47 Partially correct 20 ms 352 KB Output is partially correct
48 Partially correct 15 ms 352 KB Output is partially correct
49 Partially correct 15 ms 356 KB Output is partially correct
50 Partially correct 20 ms 312 KB Output is partially correct
51 Correct 17 ms 356 KB Output is correct
52 Partially correct 15 ms 360 KB Output is partially correct
53 Partially correct 20 ms 304 KB Output is partially correct
54 Correct 15 ms 368 KB Output is correct
55 Partially correct 14 ms 364 KB Output is partially correct
56 Partially correct 16 ms 356 KB Output is partially correct
57 Correct 7 ms 328 KB Output is correct
58 Correct 12 ms 348 KB Output is correct
59 Correct 2 ms 208 KB Output is correct
60 Correct 9 ms 324 KB Output is correct
61 Correct 10 ms 352 KB Output is correct
62 Correct 20 ms 356 KB Output is correct
63 Correct 17 ms 368 KB Output is correct