#AC001. 交互题 Interactive Sorting
交互题 Interactive Sorting
🏷️ B - Interactive Sorting
⏱️ Time Limit: 2.0 sec | 💾 Memory Limit: 1024 MiB | 🏅 Score: 300 points
📖 Problem Statement / 题目描述
This is an interactive task. Please note that these are not beginner questions. If you are a beginner, try the AtCoder Beginners Selection!
这是一个交互式任务。请注意,这不是一道入门级问题。如果您是初学者,请尝试 AtCoder Beginners Selection!
There are balls labeled with the first uppercase letters. The balls have pairwise distinct weights.
有 个球,分别用前 个大写字母标记。这些球的重量各不相同。
You are allowed to ask at most queries. In each query, you can compare the weights of two balls (see Input/Output section for details). Sort the balls in the ascending order of their weights.
您最多可以进行 次查询。在每次查询中,您可以比较两个球的重量(详见输入/输出部分)。请按重量升序对这些球进行排序。
⚠️ Constraints / 限制条件
- , or , or
🎯 Partial Score / 部分分数
There are three testsets. Each testset is worth 100 points. 共有三个测试集。每个测试集独立算分,各占 100 分:
- Testset 1: 且
- Testset 2: 且
- Testset 3: 且
🔄 Interaction Protocol / 交互格式 (输入与输出)
1. 初始输入
First, you are given and from Standard Input in the following format: 首先,你需要从标准输入中按以下格式读取 和 :
N Q
2. 发起查询
Then, you start asking queries (at most times). Each query must be printed to Standard Output in the following format: 然后,你可以开始发起查询(最多 次)。每次查询必须按以下格式输出到标准输出:
? c1 c2
注: 这里 和 都必须是前 个大写字母之一,且两者必须不同。
3. 获取反馈
Then, you are given the answer to the query from Standard Input in the following format: 接着,系统会通过标准输入返回你查询的结果:
ans
注:
ans是<或>。
- 当
ans为<时,说明球 比球 重。- 当
ans为>时,说明球 比球 重。
4. 输出答案
Finally, you must print the answer to Standard Output in the following format: 最后,你必须按以下格式将最终排序结果打印到标准输出:
! ans
注: 这里的
ans必须是一个长度为 的字符串,包含前 个大写字母各一次,且必须表示球按重量升序排列的结果。
⚖️ Judgement / 判题须知
- 刷新缓冲区 (Flush): After each output, you must flush Standard Output. Otherwise you may get TLE. (每次输出后,必须刷新标准输出。否则可能会得到 TLE。)
- 立即终止 (Terminate): After you print the answer, the program must be terminated immediately. (在打印完最终答案后,程序必须立即终止。)
- 未定义行为 (Undefined Behavior): When your output is invalid or incorrect, the behavior of the judge is undefined. (当你的输出无效或错误时,评测机的行为是未定义的,不一定会返回 WA。)
💻 Sample Code / 示例代码 (100分部分解)
Here is a sample solution for 100 points (Testset 1). Submitting it will result in a WA for the full problem. 这是一个能拿 100 分(仅通过测试集1)的示例解法。直接提交会导致最终结果为 WA。
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int main() {
int N, Q;
scanf("%d%d", &N, &Q);
string s;
for(int i = 0; i < N; i++) s += (char)('A' + i);
for(int i = 0; i < N; i++) {
for(int j = 0; j < N - 1; j++){
printf("? %c %c\n", s[j], s[j+1]);
fflush(stdout); // 必须刷新缓冲区
char ans;
scanf(" %c", &ans);
if(ans == '>') swap(s[j], s[j+1]);
}
}
printf("! %s\n", s.c_str());
fflush(stdout);
return 0;
}
📝 Sample Interaction / 交互示例
In this sample , , and the true answer is BAC.
在此示例中,假设 , ,且真实的重量升序为 BAC。
| 你的程序 (输出) | 评测系统 (输入) | 含义说明 |
|---|---|---|
3 10 |
系统给出 , | |
? A B |
你询问 A 和 B 的关系 | |
> |
系统回答:A > B (A比B重) | |
? C B |
你询问 C 和 B 的关系 | |
> |
系统回答:C > B (C比B重) | |
? A C |
你询问 A 和 C 的关系 | |
< |
系统回答:A < C (C比A重) | |
! BAC |
你得出结论并输出结果 |