namanlp's blog

By namanlp, history, 22 months ago, In English

Hi everyone,

This is my first blog post on CodeForces.

So, in this post, I will discuss about how to solve interactive problems in Rust.

This is also discussed here Solve Interactive Problems in Rust

Also, this post is inspired by https://codeforces.net/blog/entry/45307 post

So, lets go

In competitive programming contests, there can be a special type of problem called Interactive Problem. In these problems, the input is not predetermined. Rather, it is generated based on the queries. Basically, your program "sends" the "queries" and the Online judge "answers" these queries, based on which, your program generates the final answer.

And, queries are limited, off course, else the problems will be too easy.

See Basic interactive problem on CodeChef as an example.

Actually, flushing is an expensive process, and the program thread is locked while flushing. So, most of the modern programming languages make use of Buffer to store the output, and print the output later. This is called buffering.

But in the interactive problems, this is a major issue, because online judge will keep waiting indefinitely for the output, as it is stored in Buffer. So, we have to flush our outputs to online judge as soon as possible.

Flushing in Rust

If you use println!(), generally, your output will automatically be printed as soon as you call it.

For explicitly flushing the output, you first have to import using rust use std::io::{self, Write}; And then, use rust io::stdout().flush().unwrap();

for flushing.

That should be enough for solving the Interactive Problems in Rust, provided you know the logic how to solve it :D

Also, feel free to comment and correct me, and don't forget to like the post. And, Egor, your comments are most welcome!

  • Vote: I like it
  • 0
  • Vote: I do not like it