0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 18:31:59 +00:00

#6. 猜数字 2

https://loj.ac/s/963719
This commit is contained in:
Baoshuo Ren 2021-01-02 23:58:24 +08:00 committed by Baoshuo Ren
parent baf93c7cb5
commit 452deb9dc4
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
3 changed files with 129 additions and 0 deletions

31
LibreOJ/6/6.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "interaction.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> 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;
}

27
LibreOJ/6/interaction.h Normal file
View File

@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
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<int>& result) {
std::cout << "submit ";
for (std::vector<int>::const_iterator iter = result.begin(); iter != result.end(); iter++) {
std::cout << *iter << " ";
}
std::cout << std::endl
<< std::flush;
}

71
LibreOJ/6/interactor.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
std::ofstream score("score.txt");
template <typename T>
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<int> 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 + "`!");
}
}
}