Submission #221627

# Submission time Handle Problem Language Result Execution time Memory
221627 2020-04-10T14:21:29 Z rama_pang ICC (CEOI16_icc) C++14
90 / 100
169 ms 632 KB
#include "icc.h"
#include <bits/stdc++.h>
using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<vector<int>> components;

int Query(vector<int> A, vector<int> B) {
  if (A.empty() || B.empty()) return 0;
  return query(A.size(), B.size(), A.data(), B.data());
}

int QueryComponent(vector<int> A, vector<int> B) {
  vector<int> P, Q;
  for (auto i : A) {
    for (auto j : components[i]) {
      P.emplace_back(j);
    }
  }
  for (auto i : B) {
    for (auto j : components[i]) {
      Q.emplace_back(j);
    }
  }
  return Query(P, Q);
}

void Answer(int A, int B) {
  return setRoad(A, B);
}

int Split(int L, int R, vector<int> &A, vector<int> &B, int depth, bool Check) {
  if (depth == 0) {
    int mid = (L + R) / 2;
    for (int i = L; i <= mid; i++) A.emplace_back(i);
    for (int i = mid + 1; i <= R; i++) B.emplace_back(i);
    return max(mid - L + 1, R - mid);
  }
  int mid = (L + R) / 2;
  if (Check) {
    return max(Split(L, mid, A, B, depth - 1, Check), Split(mid + 1, R, A, B, depth - 1, Check));
  } else {
    vector<int> tA, tB;
    Split(L, mid, tA, tB, depth - 1, true);
    if (QueryComponent(tA, tB)) {
      return Split(L, mid, A, B, depth - 1, Check);
    } else {
      return Split(mid + 1, R, A, B, depth - 1, Check);
    }
  }
}

pair<int, int> BinarySearch(vector<int> A, vector<int> B, bool onComponent) {
  if (A.size() < B.size()) swap(A, B);
  if (A.size() == 1 && B.size() == 1) return {A[0], B[0]};

  int mid = A.size() / 2;
  vector<int> L(begin(A), begin(A) + mid), R(begin(A) + mid, end(A));

  bool res;
  if (onComponent) {
    if (R.empty() || QueryComponent(L, B)) {
      return BinarySearch(L, B, onComponent);
    } else {
      return BinarySearch(R, B, onComponent);
    }
  } else {
    if (R.empty() || Query(L, B)) {
      return BinarySearch(L, B, onComponent);
    } else {
      return BinarySearch(R, B, onComponent);
    }
  }
}

void MergeComponent(int C1, int C2) {
  vector<int> Merged;
  for (auto i : components[C1]) Merged.emplace_back(i);
  for (auto i : components[C2]) Merged.emplace_back(i);
  components[C1].clear();
  components[C2].clear();
  sort(begin(components), end(components), [&](const vector<int> &a, const vector<int> &b) {
    return a.size() > b.size();
  });

  while (components.back().empty()) components.pop_back();
  components.emplace_back(Merged);
}

void run(int N) {
  for (int i = 1; i <= N; i++) {
    components.emplace_back(vector<int>{i});
  }

  for (int i = 1; i < N; i++) {
    shuffle(begin(components), end(components), rnd);
    vector<int> A, B; // find two components that are connected by new edge
    for (int depth = 0; ; depth++) {
      int sz = Split(0, components.size() - 1, A, B, depth, true);
      if (sz == 1) break;
      int res = QueryComponent(A, B);
      A.clear(), B.clear();
      if (res) {
        Split(0, components.size() - 1, A, B, depth, false);
        break;
      }
    }

    // Binary Search to find edge's endpoint
    auto TwoComp = BinarySearch(A, B, true);
    auto Edge = BinarySearch(components[TwoComp.first], components[TwoComp.second], false);
    Answer(Edge.first, Edge.second);
    MergeComponent(TwoComp.first, TwoComp.second);
  }
}

Compilation message

icc.cpp: In function 'std::pair<int, int> BinarySearch(std::vector<int>, std::vector<int>, bool)':
icc.cpp:60:8: warning: unused variable 'res' [-Wunused-variable]
   bool res;
        ^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 512 KB Ok! 111 queries used.
2 Correct 11 ms 512 KB Ok! 106 queries used.
# Verdict Execution time Memory Grader output
1 Correct 47 ms 512 KB Ok! 626 queries used.
2 Correct 47 ms 512 KB Ok! 619 queries used.
3 Correct 48 ms 512 KB Ok! 657 queries used.
# Verdict Execution time Memory Grader output
1 Correct 153 ms 568 KB Ok! 1543 queries used.
2 Correct 169 ms 604 KB Ok! 1578 queries used.
3 Correct 158 ms 512 KB Ok! 1656 queries used.
4 Correct 165 ms 512 KB Ok! 1563 queries used.
# Verdict Execution time Memory Grader output
1 Correct 151 ms 512 KB Ok! 1650 queries used.
2 Correct 149 ms 512 KB Ok! 1649 queries used.
3 Correct 157 ms 512 KB Ok! 1687 queries used.
4 Correct 153 ms 632 KB Ok! 1564 queries used.
# Verdict Execution time Memory Grader output
1 Correct 160 ms 512 KB Ok! 1706 queries used.
2 Correct 152 ms 512 KB Ok! 1708 queries used.
3 Correct 154 ms 512 KB Ok! 1591 queries used.
4 Correct 155 ms 512 KB Ok! 1618 queries used.
5 Correct 140 ms 512 KB Ok! 1535 queries used.
6 Correct 141 ms 572 KB Ok! 1462 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 155 ms 512 KB Too many queries! 1677 out of 1625
2 Halted 0 ms 0 KB -