Submission #795808

#TimeUsernameProblemLanguageResultExecution timeMemory
795808peijarMonster Game (JOI21_monster)C++17
100 / 100
82 ms304 KiB
#include "monster.h"
#include <bits/stdc++.h>
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

// a beaten by b
bool comp(int a, int b) { return Query(b, a); }

vector<int> merge_sort(vector<int> vec) {
  if (vec.size() == 1)
    return vec;
  vector<int> l, r;
  for (int i = 0; i < (int)vec.size(); ++i) {
    if (i % 2)
      r.push_back(vec[i]);
    else
      l.push_back(vec[i]);
  }
  l = merge_sort(l);
  r = merge_sort(r);
  merge(l.begin(), l.end(), r.begin(), r.end(), vec.begin(), comp);
  return vec;
}

// vector<int> S = {13, 11, 12, 0, 3, 6, 1, 5, 7, 8, 4, 10, 2, 14, 9};

vector<int> Solve(int N) {
  vector<int> sorted(N);
  iota(sorted.begin(), sorted.end(), 0LL);
  sorted = merge_sort(sorted);

  dbg(sorted);

  vector<int> debut;
  for (int i = 0; i < min(N, 10); ++i)
    debut.push_back(sorted[i]);

  int nbDebut = debut.size();
  // for (int i = 0; i < nbDebut; ++i)
  // dbg(S[debut[i]]);
  vector<int> cntWins(nbDebut);

  for (int i = 0; i < nbDebut; ++i)
    for (int j = i + 1; j < nbDebut; ++j) {
      if (comp(debut[i], debut[j]))
        cntWins[j]++;
      else
        cntWins[i]++;
    }
  dbg(debut);
  dbg(cntWins);

  int valMin = *min_element(cntWins.begin(), cntWins.end());
  dbg(valMin);

  vector<int> withVal;
  for (int i = 0; i < nbDebut; ++i)
    if (cntWins[i] == valMin)
      withVal.push_back(i);
  assert(withVal.size() == 2);
  int posMin = debut[comp(debut[withVal[0]], debut[withVal[1]]) ? withVal[1]
                                                                : withVal[0]];

  dbg(posMin);
  vector<int> newSorted;
  newSorted.push_back(posMin);
  for (int x : sorted)
    if (x != posMin)
      newSorted.push_back(x);

  sorted = move(newSorted);

  for (int deb = 1; deb < N;) {
    int fin = deb;
    while (comp(sorted[deb - 1], sorted[fin]))
      ++fin;
    reverse(sorted.begin() + deb, sorted.begin() + fin + 1);
    deb = fin + 1;
  }

  vector<int> T(N);
  for (int i = 0; i < N; i++)
    T[sorted[i]] = i;

  return T;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...