Submission #442759

# Submission time Handle Problem Language Result Execution time Memory
442759 2021-07-08T21:11:25 Z peijar Mechanical Doll (IOI18_doll) C++17
100 / 100
260 ms 15096 KB
#include "doll.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> exitFixed;
vector<int> exitEven, exitOdd;
int nbSwaps;

bool allRoot(vector<int> e) {
  for (auto v : e)
    if (v != -1)
      return false;
  return true;
}
int build(vector<int> elements) {
  int nbElem = elements.size();
  assert(nbElem > 1);
  if (nbElem == 2) {
    ++nbSwaps;
    exitEven.push_back(elements[0]);
    exitOdd.push_back(elements[1]);
    return -nbSwaps;
  }

  vector<int> even, odd;
  for (int i = 0; i < nbElem; ++i)
    (i % 2 ? odd : even).push_back(elements[i]);
  int ret = ++nbSwaps;

  exitEven.push_back(-1e9);
  exitOdd.push_back(-1e9);
  if (allRoot(even))
    exitEven[ret - 1] = -1;
  else
    exitEven[ret - 1] = build(even);
  if (allRoot(odd))
    exitOdd[ret - 1] = -1;
  else if (odd.size() > 1)
    exitOdd[ret - 1] = build(odd);
  else
    exitOdd[ret - 1] = odd.back();
  return -ret;
}

vector<int> posFeuilles(vector<int> restants) {
  int nbRestants = restants.size();
  assert(__builtin_popcount(nbRestants));
  if (nbRestants == 1)
    return {restants[0]};
  vector<int> even, odd;
  for (int i = 0; i < nbRestants; ++i)
    (i % 2 ? odd : even).push_back(restants[i]);
  auto lft = posFeuilles(even), rgt = posFeuilles(odd);
  for (auto v : rgt)
    lft.push_back(v);
  return lft;
}

void create_circuit(int nbFixes, vector<int> ordre) {
  int nbEtapes = ordre.size();
  exitFixed.resize(nbFixes + 1);
  exitFixed[0] = ordre[0];
  if (nbEtapes == 1) {
    exitFixed[ordre[0]] = 0;
    answer(exitFixed, exitEven, exitOdd);
    return;
  }

  int k = 0;
  while ((1LL << (k + 1)) < nbEtapes)
    k++;
  int restant = (1 << (k + 1)) - nbEtapes;
  if ((1 << k) == nbEtapes)
    restant = 0;
  vector<int> v;
  for (int i = 0; i < restant + nbEtapes; ++i)
    v.push_back(i);
  auto pos = posFeuilles(v);
  cerr << "Val : " << k << ' ' << restant << endl;
  vector<int> toBuild(restant + nbEtapes);

  for (int i = 0; i < restant; ++i)
    toBuild[pos[i]] = -1;
  vector<int> restantes;
  for (int i = restant; i < nbEtapes + restant; ++i)
    restantes.push_back(pos[i]);
  sort(restantes.begin(), restantes.end());
  for (int i = 0; i < nbEtapes; ++i)
    toBuild[restantes[i]] = i + 1 < nbEtapes ? ordre[i + 1] : 0;
  for (int i = 1; i <= nbFixes; ++i)
    exitFixed[i] = -1;
  build(toBuild);
  cerr << "count switch : " << exitEven.size() << endl;
  assert((int)exitEven.size() <= nbEtapes + (int)log2(nbEtapes));
  assert(nbSwaps <= 2 * nbEtapes);
  answer(exitFixed, exitEven, exitOdd);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 72 ms 5752 KB Output is correct
3 Correct 98 ms 6720 KB Output is correct
4 Correct 1 ms 260 KB Output is correct
5 Correct 11 ms 1356 KB Output is correct
6 Correct 119 ms 9012 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 72 ms 5752 KB Output is correct
3 Correct 98 ms 6720 KB Output is correct
4 Correct 1 ms 260 KB Output is correct
5 Correct 11 ms 1356 KB Output is correct
6 Correct 119 ms 9012 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 136 ms 9712 KB Output is correct
9 Correct 186 ms 13112 KB Output is correct
10 Correct 231 ms 15096 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 72 ms 5752 KB Output is correct
3 Correct 98 ms 6720 KB Output is correct
4 Correct 1 ms 260 KB Output is correct
5 Correct 11 ms 1356 KB Output is correct
6 Correct 119 ms 9012 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 136 ms 9712 KB Output is correct
9 Correct 186 ms 13112 KB Output is correct
10 Correct 231 ms 15096 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 231 ms 14924 KB Output is correct
15 Correct 185 ms 11828 KB Output is correct
16 Correct 229 ms 14096 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 231 ms 14228 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 125 ms 7952 KB Output is correct
3 Correct 209 ms 11408 KB Output is correct
4 Correct 218 ms 12912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 125 ms 7952 KB Output is correct
3 Correct 209 ms 11408 KB Output is correct
4 Correct 218 ms 12912 KB Output is correct
5 Correct 259 ms 13860 KB Output is correct
6 Correct 222 ms 13584 KB Output is correct
7 Correct 260 ms 13716 KB Output is correct
8 Correct 232 ms 13468 KB Output is correct
9 Correct 181 ms 11324 KB Output is correct
10 Correct 219 ms 13452 KB Output is correct
11 Correct 219 ms 13208 KB Output is correct
12 Correct 174 ms 11332 KB Output is correct
13 Correct 127 ms 8584 KB Output is correct
14 Correct 178 ms 11756 KB Output is correct
15 Correct 184 ms 11652 KB Output is correct
16 Correct 6 ms 588 KB Output is correct
17 Correct 127 ms 8268 KB Output is correct
18 Correct 127 ms 8196 KB Output is correct
19 Correct 176 ms 11416 KB Output is correct
20 Correct 223 ms 13384 KB Output is correct
21 Correct 218 ms 13080 KB Output is correct
22 Correct 223 ms 13272 KB Output is correct