Submission #875483

# Submission time Handle Problem Language Result Execution time Memory
875483 2023-11-19T21:43:41 Z LucaLucaM ICC (CEOI16_icc) C++17
61 / 100
142 ms 848 KB
#include "icc.h"
#ifdef LOCAL
#include "grader.cpp"
#endif // LOCAL
#include <vector>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <functional>
#include <random>
#ifndef LOCAL
#define assert //assert
#endif // LOCAL

using vi = std::vector<int>;

const int NMAX = 100;

int p[NMAX + 1];

int root(int u) {
  if (p[u] == u) {
    return u;
  }
  return p[u] = root(p[u]);
}

void join(int u, int v) {
  u = root(u), v = root(v);
  p[u] = v;
}

int ask(vi a, vi b) {
  if (a.empty() || b.empty()) {
    return 0;
  }
  int sz_a = (int) a.size(), sz_b = (int) b.size();
  int A[sz_a], B[sz_b];
  for (int i = 0; i < sz_a; i++) {
    A[i] = a[i];
  }
  for (int i = 0; i < sz_b; i++) {
    B[i] = b[i];
  }
  return query(sz_a, sz_b, A, B);
}

int ask(std::vector<vi> a, vi b) {
  if (a.empty() || b.empty()) {
    return 0;
  }
  std::vector<int> A;
  for (const auto &w : a) {
    for (const auto &x : w) {
      A.push_back(x);
    }
  }
  return ask(A, b);
}
int ask(vi a, std::vector<vi> b) {
  if (a.empty() || b.empty()) {
    return 0;
  }
  std::vector<int> B;
  for (const auto &w : b) {
    for (const auto &x : w) {
      B.push_back(x);
    }
  }
  return ask(a, B);
}

int ask(std::vector<vi> a, std::vector<vi> b) {
  if (a.empty() || b.empty()) {
    return 0;
  }
  std::vector<int> B;
  for (const auto &w : b) {
    for (const auto &x : w) {
      B.push_back(x);
    }
  }
  return ask(a, B);
}

vi operator + (vi a, vi b) {
  vi c = a;
  for (const auto &x : b) {
    c.push_back(x);
  }
  return c;
}

/**
setRoad() -> alege doua cc si le da join
daca eu aflu aceste doua cc => pot face doua cb si o sa am 2 * log query uri
daca fac brut si mereu incerc sa aflu o cc noua =>
in total ~n(n - 1) / 2 query uri ca sa aflu cc desi cred ca e putin mai bine ceva gen mai trebuie scazut 1 * n
intre doua cc sigur nu am muchie
daca bag gen query pe bucati de radical cu ce e in afara ar iesi si ar fi 2 * sqrt(m) cred, m = #cc
ok acm am realizat ca asta e proasta
spamam random shuffle pana prima jumate are una si a doua jumate are alta
putem face chestia cu

000
001
010
011
100
101
110
111

**/

std::mt19937 rng(42); // cica a intrat bine asta la ejoi

void run(int n) {
  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }
  for (int k = 1; k < n; k++) {
    vi kk[n + 1];
    int m = n - (k - 1);
    for (int i = 1; i <= n; i++) {
      kk[root(i)].push_back(i);
    }
    vi care;
    for (int i = 1; i <= n; i++) {
      if (!kk[i].empty()) {
        care.push_back(i);
      }
    }
    int sz = (int) care.size();
    std::vector<vi> A, B;
    for (int bt = 0; bt <= std::__lg(sz); bt++) {
      A.clear(), B.clear();
      for (int i = 0; i < sz; i++) {
        if (i & (1 << bt)) {
          A.push_back(kk[care[i]]);
        } else {
          B.push_back(kk[care[i]]);
        }
      }
      if (ask(A, B) == 1) {
        break;
      }
    }
    assert(ask(A, B) == 1);

    std::function<std::pair<int, int>(vi, vi) > get = [&] (vi a, vi b) { // SUS
      assert(!a.empty() && !b.empty());
      if ((int) a.size() == 1 && (int) b.size() == 1) {
        return std::pair<int, int> {a[0], b[0]};
      }
      if ((int) a.size() == 1) {
        vi as;
        int i = 0;
        for (i = 0; i < (int) b.size() / 2; i++) {
          as.push_back(b[i]);
        }
        if (ask(a, as) == 1) {
          assert((int) as.size() < (int) b.size());
          return get(a, as);
        } else {
          as.clear();
          while (i < (int) b.size()) {
            as.push_back(b[i++]);
          }
          assert((int) as.size() < (int) b.size());
          return get(a, as);
        }
      } else {
        vi as;
        int i = 0;
        for (i = 0; i < (int) a.size() / 2; i++) {
          as.push_back(a[i]);
        }
        if (ask(as, b) == 1) {
          assert((int) as.size() < (int) a.size());
          return get(as, b);
        } else {
          as.clear();
          while (i < (int) a.size()) {
            as.push_back(a[i++]);
          }
          assert((int) as.size() < (int) a.size());
          return get(as, b);
        }
      }
    };

    std::function<std::pair<vi, vi>(std::vector<vi>, std::vector<vi>) > get2 = [&] (std::vector<vi> a, std::vector<vi> b) { // SUS
//      std::cout << "??? " << (int) a.size() << ' ' << (int) b.size() << '\n';
      assert(!a.empty() && !b.empty());
      if ((int) a.size() == 1 && (int) b.size() == 1) {
        return std::pair<vi, vi> {a[0], b[0]};
      }
      if ((int) a.size() == 1) {
        std::vector<vi> as;
        int i = 0;
        for (i = 0; i < (int) b.size() / 2; i++) {
          as.push_back(b[i]);
        }
        if (ask(a, as) == 1) {
          assert((int) as.size() < (int) b.size());
          return get2(a, as);
        } else {
          as.clear();
          while (i < (int) b.size()) {
            as.push_back(b[i++]);
          }
          assert((int) as.size() < (int) b.size());
          return get2(a, as);
        }
      } else {
        std::vector<vi> as;
        int i = 0;
        for (i = 0; i < (int) a.size() / 2; i++) {
          as.push_back(a[i]);
        }
        if (ask(as, b) == 1) {
          assert((int) as.size() < (int) a.size());
          return get2(as, b);
        } else {
          as.clear();
          while (i < (int) a.size()) {
            as.push_back(a[i++]);
          }
          assert((int) as.size() < (int) a.size());
          return get2(as, b);
        }
      }
    };

    std::pair<vi, vi> res1 = get2(A, B);
//    continue;
    std::pair<int, int> res = get(res1.first, res1.second);
    join(res.first, res.second);
    setRoad(res.first, res.second);
  }
}

Compilation message

icc.cpp:14: warning: "assert" redefined
   14 | #define assert //assert
      | 
In file included from /usr/include/c++/10/cassert:44,
                 from icc.cpp:7:
/usr/include/assert.h:92: note: this is the location of the previous definition
   92 | #  define assert(expr)       \
      | 
icc.cpp: In function 'void run(int)':
icc.cpp:126:9: warning: unused variable 'm' [-Wunused-variable]
  126 |     int m = n - (k - 1);
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 604 KB Ok! 128 queries used.
2 Correct 5 ms 604 KB Ok! 129 queries used.
# Verdict Execution time Memory Grader output
1 Correct 28 ms 708 KB Ok! 680 queries used.
2 Correct 30 ms 676 KB Ok! 717 queries used.
3 Correct 40 ms 604 KB Ok! 760 queries used.
# Verdict Execution time Memory Grader output
1 Correct 98 ms 752 KB Ok! 1954 queries used.
2 Correct 88 ms 728 KB Ok! 1717 queries used.
3 Correct 142 ms 604 KB Ok! 2036 queries used.
4 Correct 98 ms 736 KB Ok! 1935 queries used.
# Verdict Execution time Memory Grader output
1 Correct 101 ms 700 KB Ok! 1965 queries used.
2 Correct 101 ms 848 KB Ok! 1939 queries used.
3 Correct 106 ms 764 KB Ok! 1991 queries used.
4 Correct 102 ms 728 KB Ok! 1987 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 117 ms 740 KB Too many queries! 1995 out of 1775
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 98 ms 772 KB Too many queries! 1943 out of 1625
2 Halted 0 ms 0 KB -