An interactor is used to interact with contestants' programs in interactive problems. An introduction to interactive problems can be found in Problem Types - Interactive Problems.
Note
Testlib only supports Codeforces-style interactive problems, in which two programs interact. It does not support NOI-style problems, in which a function written by the contestant interacts with another function.
Please read General before reading the following.
Testlib provides a special stream std::fstream tout for the interactor. It is a log stream: the interactor can write to it, and the checker can read it using ouf.
In the interactor, we read the problem's test data from inf, write the standard input of the contestant's program (and the standard solution) to stdout (online), read the contestant's output from ouf (online), and read the standard output from ans (online).
If the interactor returns an ok status, the checker, if any, takes over and checks the validity of the answer.
Usage
Windows:
interactor.exe <Input_File> <Output_File> [<Answer_File> [<Result_File> [-appes]]],
Linux:
./interactor.out <Input_File> <Output_File> [<Answer_File> [<Result_File> [-appes]]],
A simple example
Problem
The interactor randomly selects an integer in the range . You need to write a program to guess it, and may ask at most times about an integer in the range .
The interactor returns:
1: the query is equal to the answer; your program should stop querying.
0: the query is less than the answer.
2: the query is greater than the answer.
Note that in this problem we do not need ans, because we do not need to compare the standard output with it; other problems may require this.
int main(int argc, char** argv) {
registerInteraction(argc, argv);
int n = inf.readInt(); // selected number
cout.flush(); // flush the buffer
int left = 50;
bool found = false;
while (left > 0 && !found) {
left--;
int a = ouf.readInt(1, 1000000000); // query
if (a < n)
cout << 0 << endl;
else if (a > n)
cout << 2 << endl;
else
cout << 1 << endl, found = true;
cout.flush();
}
if (!found) quitf(_wa, "couldn't guess the number with 50 questions");
ouf.readEof();
quitf(_ok, "guessed the number with %d questions!", 50 - left);
}
This article is mainly translated from Interactors with testlib.h - Codeforces. The GitHub repository for testlib.h is MikeMirzayanov/testlib.