Problem link = Link
I’m solving an interactive problem, and I’m encountering an issue where my solution works perfectly when I use direct integer inputs/outputs for queries. However, when I attempt to format the query using string concatenation (e.g., “? ” + to_string(x) + ” ” + to_string(y)), the solution fails.
In both cases, I ensure that the output is flushed using flush() after every query. Yet, the first approach doesn’t work as expected. Could the string concatenation or output formatting be causing a timing issue or mismatched expectations in the interactive environment? How can I ensure my solution works consistently?
int query(string s) {
FastIO::println(s);
FastIO::flush();
return FastIO::nextInt();
}
void solve() {
int n = FastIO::nextInt();
vector<pair<int, int>> list;
for (int x = 2; x <= n; x++) {
int y = 1, pre = -1;
while (pre != y) {
pre = y;
y = query("? " + to_string(x) + " " + to_string(y));
}
list.push_back({x, pre});
}
FastIO::print("! ");
for (auto &e : list) {
FastIO::print(to_string(e.first) + " " + to_string(e.second) + " ");
}
FastIO::println("");
FastIO::flush();
}
int main() {
int t = FastIO::nextInt();
while (t--) {
solve();
}
return 0;
}
This code did not work But the next code worked
int query(int x, int y) {
FastIO::println("? " + to_string(x) + " " + to_string(y));
FastIO::flush();
return FastIO::nextInt();
}
void solve() {
int n = FastIO::nextInt();
vector<pair<int, int>> list;
for (int x = 2; x <= n; x++) {
int y = 1, pre = -1;
while (pre != y) {
pre = y;
y = query(y, x);
}
list.push_back({x, pre});
}
FastIO::print("! ");
for (auto &e : list) {
FastIO::print(to_string(e.first) + " " + to_string(e.second) + " ");
}
FastIO::println("");
FastIO::flush();
}
int main() {
int t = FastIO::nextInt();
while (t--) {
solve();
}
return 0;
}
I was expecting both codes to give correct answer.
The java code for both codes was also giving same results.
SOUMADIP MISHRA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2