답안 #875479

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875479 2023-11-19T21:10:08 Z LucaLucaM CEOI16_icc (CEOI16_icc) C++17
18 / 100
268 ms 904 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>
#ifndef LOCAL
#define assert //assert
#endif // LOCAL

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(std::vector<int> a, std::vector<int> 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);
}

std::vector<int> operator + (std::vector<int> a, std::vector<int> b) {
  std::vector<int> 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
**/

void run(int n) {
  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }
  for (int k = 1; k < n; k++) {
    bool ok = false;
    std::vector<int> kk[n + 1];
    for (int i = 1; i <= n; i++) {
      kk[root(i)].push_back(i);
    }
    int m = n - (k - 1);

    auto exclude = [&] (int bucket) {
      std::vector<int> ret;
      for (int i = 1; i <= n; i++) {
        if (i != bucket)
          ret = ret + kk[i];
      }
      return ret;
    };

    int bucket = -1;
    for (int i = 1; i <= n && bucket == -1; i++) {
      if (!kk[i].empty()) {
        if (ask(kk[i], exclude(i))) {
          bucket = i;
        }
      }
    }
    assert(bucket != -1);

    std::vector<int> care;
    for (int i = bucket + 1; i <= n; i++) {
      if (!kk[i].empty()) {
        care.push_back(i);
      }
    }

    auto get_bucket2_bs = [&] (int bucket1) {
      int l = 0, r = (int) care.size() -1, sol = -1;
      while (l <= r) {
        int mid = (l + r) / 2;
        std::vector<int> intreb;
        for (int i = 0; i <= mid; i++) {
          intreb = intreb + kk[i];
        }
        if (ask(kk[bucket1], intreb) == 1) {
          r = mid - 1;
          sol = mid;
        } else {
          l = mid + 1;
        }
      }
      return sol;
    };

    auto get_bucket2_brute = [&] (int bucket1) {
      for (int i = bucket1 + 1; i <= n; i++) {
        if (ask(kk[i], exclude(i)) == 1) {
          return i;
        }
      }
    };

    int sol = get_bucket2_brute(bucket);
    assert(sol != -1);
    int bucket2 = sol;
    assert(ask(kk[bucket], kk[bucket2]) == 1);

    std::function<std::pair<int, int>(std::vector<int>, std::vector<int>) > get = [&] (std::vector<int> a, std::vector<int> 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) {
        std::vector<int> as;
        int i = 0;
        for (i = 0; i < (int) b.size() - i - 1; i++) {
          as.push_back(b[i]);
        }
        if (ask(a, as) == 1) {
          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 {
        std::vector<int> as;
        int i = 0;
        for (i = 0; i < (int) a.size() - i - 1; i++) {
          as.push_back(a[i]);
        }
        if (ask(as, b) == 1) {
          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::vector<int> A, B;
    for (const auto &x : kk[bucket]) {
      A.push_back(x);
    }
    for (const auto &x : kk[bucket2]) {
      B.push_back(x);
    }
    assert(!A.empty() && !B.empty());
    std::pair<int, int> res = get(A, B);
    join(res.first, res.second);
    setRoad(res.first, res.second);
  }
}

Compilation message

icc.cpp:13: warning: "assert" redefined
   13 | #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:69:10: warning: unused variable 'ok' [-Wunused-variable]
   69 |     bool ok = false;
      |          ^~
icc.cpp:74:9: warning: unused variable 'm' [-Wunused-variable]
   74 |     int m = n - (k - 1);
      |         ^
icc.cpp:102:10: warning: variable 'get_bucket2_bs' set but not used [-Wunused-but-set-variable]
  102 |     auto get_bucket2_bs = [&] (int bucket1) {
      |          ^~~~~~~~~~~~~~
icc.cpp: In lambda function:
icc.cpp:126:5: warning: control reaches end of non-void function [-Wreturn-type]
  126 |     };
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 600 KB Ok! 174 queries used.
2 Correct 7 ms 604 KB Ok! 172 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 852 KB Ok! 1549 queries used.
2 Correct 68 ms 636 KB Ok! 1548 queries used.
3 Correct 67 ms 604 KB Ok! 1554 queries used.
# 결과 실행 시간 메모리 Grader output
1 Incorrect 268 ms 656 KB Number of queries more than 4500 out of 2250
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 239 ms 652 KB Number of queries more than 4000 out of 2000
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 201 ms 652 KB Number of queries more than 3550 out of 1775
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 193 ms 904 KB Number of queries more than 3250 out of 1625
2 Halted 0 ms 0 KB -