Submission #582571

# Submission time Handle Problem Language Result Execution time Memory
582571 2022-06-24T06:07:54 Z Plurm Permutation (APIO22_perm) C++17
0 / 100
1 ms 212 KB
#include "perm.h"
#include <algorithm>
#include <iostream>
 
class intperm {
public:
  std::vector<int> perm;
  long long value;
  intperm(std::vector<int> perm = {}, long long value = 1)
      : perm(perm), value(value) {}
  std::pair<int, int> get_bounds() {
    return std::make_pair(*min_element(perm.begin(), perm.end()),
                          *max_element(perm.begin(), perm.end()));
  }
  friend bool operator<(const intperm &a, const intperm &b) {
    return a.value < b.value;
  }
  friend intperm operator+(intperm a, intperm b) {
    intperm nxt;
    if (b < a) {
      std::swap(a.perm, b.perm);
      std::swap(a.value, b.value);
    }
    nxt.perm = b.perm;
    int offs = (a.perm.empty() ? 0 : a.get_bounds().second + 1);
    for (int &x : nxt.perm)
      x += offs;
    nxt.perm.insert(nxt.perm.end(), a.perm.begin(), a.perm.end());
    nxt.value = a.value + b.value - 1;
    return nxt;
  }
  friend intperm operator*(intperm a, intperm b) {
    intperm nxt;
    if (b < a) {
      std::swap(a.perm, b.perm);
      std::swap(a.value, b.value);
    }
    nxt.perm = b.perm;
    int offs = (a.perm.empty() ? 0 : a.get_bounds().second + 1);
    for (int &x : nxt.perm)
      x += offs;
    nxt.perm.insert(nxt.perm.begin(), a.perm.begin(), a.perm.end());
    nxt.value = a.value * b.value;
    return nxt;
  }
};
 
const intperm ONE({}, 1);
const intperm TWO({0}, 2);
const intperm THREE({1,0}, 3);
const intperm FOUR({0,1}, 4);
std::vector<int> construct_permutation(long long k) {
  intperm ans;
  int msb = 63 - __builtin_clzll(k);
  msb = (msb + 1) / 2 * 2;
  switch ((k & (3ll << (msb - 2ll))) >> (msb - 2ll)) {
    case 1ll:
      ans = ONE;
      break;
    case 2ll:
      ans = TWO;
      break;
    case 3ll:
      ans = THREE;
      break;
  }
  for (long long i = msb - 4; i >= 0; i -= 2) {
    ans = ans * FOUR;
    switch ((k & (3ll << i)) >> i) {
      case 1ll:
        ans = ans + TWO;
        break;
      case 2ll:
        ans = ans + THREE;
        break;
      case 3ll:
        ans = ans + FOUR;
        break;
    }
  }
  return ans.perm;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Integer 0 violates the range [1, 5000]
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Integer 0 violates the range [1, 5000]
3 Halted 0 ms 0 KB -