CF2206A.Compare Suffixes

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The judge program possesses a hidden string SS of length nn consisting only of lowercase Latin letters (a–z). You cannot access the string. At the start, only the length nn is given to you.

Your task is to determine the order of all nn suffixes using a limited number of queries.

For an integer kk (1≤k≤n1 \le k \le n), let S(k)S(k) denote the suffix of SS starting at the kk-th character. In particular, S(1)=SS(1)=S.

In a single query, you specify two distinct integers ii and jj. The judge program compares S(i)S(i) and S(j)S(j) lexicographically and returns whether S(i)<S(j)S(i) \lt S(j) or S(i)>S(j)S(i) \gt S(j) to you. Note that ties never occur for i≠ji\not= j because all suffixes are distinct.

Find a permutation (p1,p2,…,pn)(p_1,p_2,\ldots,p_n) of (1,2,…,n)(1,2,\ldots,n) such that S(p1)<S(p2)<⋯<S(pn)S(p_1) \lt S(p_2) \lt \cdots \lt S(p_n).

Interaction

The first line of input contains the integer nn (2≤n≤10002 \le n \le 1000).

To issue a query, your program should write a line of the form "query ii jj" (1≤i≤n;1≤j≤n;i≠j1 \le i \le n; 1 \le j \le n; i \not= j). After that, an input line containing first or second becomes available. A line containing first means S(i)<S(j)S(i) \lt S(j), while a line containing second means S(i)>S(j)S(i) \gt S(j).

Once you determine the order of the suffixes, your program should write a line of the form "answer p1p_1 p2p_2 …\ldots pnp_n". After that, the interaction stops and your program should terminate without extra output.

Your program may issue at most 62606260 queries. If your program issues more than 62606260 queries, it will be judged as "Wrong Answer."

It is guaranteed that the hidden string SS consists only of lowercase letters (a–z).

Notes on interactive judging:

  • The evaluation is non-adversarial, meaning that the string SS is chosen in advance rather than in response to your queries.
  • Do not forget to flush output buffers after writing.
  • You are provided with a command-line tool for local testing, together with input files corresponding to the sample interactions. You can download these files. The tool has comments at the top to explain its use.

评测程序持有一个长度为 nn 的隐藏字符串 SS,该字符串仅由小写拉丁字母(a–z)组成。你无法直接访问该字符串。初始时,你仅获知其长度 nn。

你的任务是通过有限次数的查询,确定该字符串全部 nn 个后缀的字典序排列顺序。

对整数 kk(1≤k≤n1 \le k \le n),记 S(k)S(k) 表示从第 kk 个字符开始的 SS 的后缀。特别地,S(1)=SS(1) = S。

在一次查询中,你需要指定两个互异的整数 ii 和 jj。评测程序将对 S(i)S(i) 与 S(j)S(j) 进行字典序比较,并向你返回 S(i)<S(j)S(i) \lt S(j) 或 S(i)>S(j)S(i) \gt S(j)。注意:由于所有后缀互不相同,当 i≠ji \ne j 时,不会出现相等的情况。

请找出一个 (1,2,…,n)(1,2,\ldots,n) 的排列 (p1,p2,…,pn)(p_1,p_2,\ldots,p_n),使得 S(p1)<S(p2)<⋯<S(pn)S(p_1) \lt S(p_2) \lt \cdots \lt S(p_n)。

交互方式

输入的第一行包含整数 nn(2≤n≤10002 \le n \le 1000)。

要发起一次查询,你的程序应输出一行,格式为 "query $i$ $j$"(其中 1≤i≤n1 \le i \le n,1≤j≤n1 \le j \le n,且 i≠ji \ne j)。随后,将有一行输入可用,内容为 first 或 second:若为 first,表示 S(i)<S(j)S(i) \lt S(j);若为 second,表示 S(i)>S(j)S(i) \gt S(j)。

一旦你确定了所有后缀的排序顺序,你的程序应输出一行,格式为 "answer $p_1$ $p_2$ $\ldots$ $p_n$"。此后交互结束,你的程序应立即终止,不得输出额外内容。

你的程序最多可发出 62606260 次查询。若查询次数超过 62606260,将被判为“答案错误”。

保证隐藏字符串 SS 仅由小写字母(a–z)组成。

关于交互式评测的注意事项:

  • 评测是非对抗性的,即字符串 SS 是预先选定的,而非根据你的查询动态生成。
  • 输出后务必刷新输出缓冲区。
  • 我们为你提供了一个命令行本地测试工具,以及对应样例交互的输入文件。你可以下载这些文件。该工具顶部附有注释,说明其使用方法。

输入输出样例

  • 输入#1

    4
    
    first
    
    second
    
    first

    输出#1

    query 2 1
    
    query 2 4
    
    query 1 3
    
    answer 4 2 1 3

说明/提示

Explanation for the sample interaction #1

In this sample, S=icpcS=\texttt{icpc} is assumed. The order of four suffixes is S(4)<S(2)<S(1)<S(3)S(4) \lt S(2) \lt S(1) \lt S(3) because c<cpc<icpc<pc\texttt{c} \lt \texttt{cpc} \lt \texttt{icpc} \lt \texttt{pc}.

In the first and third query, first is returned because S(2)<S(1)S(2) \lt S(1) and S(1)<S(3)S(1) \lt S(3). In the second query, second is returned because S(2)>S(4)S(2) \gt S(4). With these responses, you can determine the ordering.

样例交互 #1 的说明

在本样例中,假设 S=icpcS=\texttt{icpc}。四个后缀的顺序为 S(4)<S(2)<S(1)<S(3)S(4) \lt S(2) \lt S(1) \lt S(3),这是因为 c<cpc<icpc<pc\texttt{c} \lt \texttt{cpc} \lt \texttt{icpc} \lt \texttt{pc}。

在第一次和第三次查询中,返回 first,因为 S(2)<S(1)S(2) \lt S(1) 且 S(1)<S(3)S(1) \lt S(3);在第二次查询中,返回 second,因为 S(2)>S(4)S(2) \gt S(4)。通过这些响应,你可以确定全部后缀的顺序。

输入解题思路,AI测评打分。不知道怎么写?

首页