how to solve this problem by th other solution? #include <bits/stdc++.h> class Judger;…
- User
how to solve this problem by th other solution? #include <bits/stdc++.h>
class Judger;
class Solution { public: virtual void init(Judger *) = 0; virtual std::vector solve() = 0; };
class Judger { public: Judger(int num = 100, int bad_num = 30, int seed = rd()) { this->num = num; query_num = 0; std::mt19937 gen(seed);
if (bad_num == -1) { std::bernoulli_distribution d(0.5); for (int i = 0; i < num; ++i) { peoples.push_back(d(gen)); } return; } for (int i = 0; i < num; ++i) { peoples.push_back(i < bad_num); } std::shuffle(peoples.begin(), peoples.end(), gen); } int query(const std::vector<int> people_idx) { query_num++; int ret = 0; for (int i : people_idx) { ret += peoples[i]; } return ret; } int solution_query_num(Solution *solution) { solution->init(this); if (!verify_ans(solution->solve())) { return -1; } int ret = get_query_num(); reset_query_num(); return ret; }private: void reset_query_num() { query_num = 0; }
int get_query_num() { return query_num; } bool verify_ans(const std::vector<int> res) { if (res != peoples) { std::cout<<"res: "; for(int i = 0; i<num; i++) { std::cout<<res[i]; } std::cout<<std::endl; std::cout<<"ans: "; for(int i = 0; i<num; i++) { std::cout<<peoples[i]; } return false; } return true; }public: int num;
private: static std::random_device rd; int query_num; std::vector peoples; };
class RecurseSolution : public Solution { public: void init(Judger *judger) { num = judger->num; res.resize(num); this->judger = judger; }
std::vector<int> solve() { std::vector<int> vec(num); std::iota(vec.begin(), vec.end(), 0); solve_helper(0, num - 1, judger->query(vec)); return res; }private: void solve_helper(int l, int r, int m) { if (m == 0) { return; } if (m == r - l + 1) { for (int i = l; i <= r; i ++) { res[i] = 1; } return; } int mid = (l + r) / 2; std::vector vec(mid - l + 1); std::iota(vec.begin(), vec.end(), l); int lm = judger->query(vec); solve_helper(l, mid, lm); solve_helper(mid+1, r, m - lm); }
private: int num; std::vector res; Judger *judger; };
std::random_device Judger::rd; int main() { Judger* judger = new Judger(100, -1);
Solution* recurseSolution = new RecurseSolution(); int recurseStep = judger->solution_query_num(recurseSolution); std::cout << "result: " << (recurseStep != -1 ? "pass" : "failed") << std::endl; std::cout << "step: " << recurseStep << std::endl; return 0;}
- Assistant