diff --git a/LibreOJ/6/6.cpp b/LibreOJ/6/6.cpp new file mode 100644 index 00000000..2d3b70c1 --- /dev/null +++ b/LibreOJ/6/6.cpp @@ -0,0 +1,31 @@ +#include "interaction.h" +#include + +using namespace std; + +int n; +vector ans; + +int main() { + n = get_num(); + for (int i = 0; i < n; i++) { + int l = 0, r = 1000000; + + while (l <= r) { + int mid = (l + r) >> 1, feedback = guess(i, mid); + + if (!feedback) { + ans.emplace_back(mid); + break; + } else { + if (feedback > 0) { + r = mid - 1; + } else { + l = mid + 1; + } + } + } + } + submit(ans); + return 0; +} \ No newline at end of file diff --git a/LibreOJ/6/interaction.h b/LibreOJ/6/interaction.h new file mode 100644 index 00000000..25dce1f3 --- /dev/null +++ b/LibreOJ/6/interaction.h @@ -0,0 +1,27 @@ +#include +#include + +int get_num() { + std::cout << "get_num" << std::endl + << std::flush; + int ret; + std::cin >> ret; + return ret; +} + +int guess(int index, int x) { + std::cout << "guess " << index << " " << x << std::endl + << std::flush; + int ret; + std::cin >> ret; + return ret; +} + +void submit(const std::vector& result) { + std::cout << "submit "; + for (std::vector::const_iterator iter = result.begin(); iter != result.end(); iter++) { + std::cout << *iter << " "; + } + std::cout << std::endl + << std::flush; +} diff --git a/LibreOJ/6/interactor.cpp b/LibreOJ/6/interactor.cpp new file mode 100644 index 00000000..47233866 --- /dev/null +++ b/LibreOJ/6/interactor.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +std::ofstream score("score.txt"); + +template +inline void assert(const T& condition, const std::string& message) { + if (!condition) { + std::cerr << message << std::endl; + score << -1 << std::endl; + exit(0); + } +} + +int main() { + std::ifstream datin("input"); + int n, nguess = 0; + datin >> n; + std::vector nums(n); + for (int i = 0; i < n; i++) { + datin >> nums[i]; + } + + while (true) { + std::string cmd; + std::cin >> cmd; + if (cmd == "get_num") { + std::cout << n << std::endl + << std::flush; + } else if (cmd == "guess") { + nguess++; + int index, val; + assert(std::cin >> index >> val, "Invalid `index` or `val` in `guess` command!"); + assert(0 <= index && index < n, "`index` must in [0, n)!"); + + if (val < nums[index]) { + std::cout << -1 << std::endl + << std::flush; + } else if (val > nums[index]) { + std::cout << 1 << std::endl + << std::flush; + } else { + std::cout << 0 << std::endl + << std::flush; + } + } else if (cmd == "submit") { + bool wrongAnswer = false; + for (int i = 0; i < n; i++) { + int x; + assert(std::cin >> x, "Can't read the " + std::to_string(i) + "-th number!"); + + if (x != nums[i] && !wrongAnswer) { + wrongAnswer = true; + std::cerr << "First differ on the " + std::to_string(i) + "-th number!" << std::endl; + score << 0 << std::endl; + } + } + + if (!wrongAnswer) { + score << std::max(std::min(100.0, (950.0 - ((double)nguess / n - 100.0)) / 950.0 * 100.0), 0.0) << std::endl; + std::cerr << "nguess = " + std::to_string(nguess) << std::endl; + } + + exit(0); + } else { + assert(false, "Invalid command `" + cmd + "`!"); + } + } +}