CF730B.Minimum and Maximum
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush().
In this problem, you need to find maximal and minimal elements of an array. What could be simpler?
You can imagine that the jury has an array, and initially you know the only number n — array's length.
Array's elements are numbered from 1 to n. You are allowed to compare two elements of the array by using their indices i and j. There are three possible responses to this query: '<' (if a__i is less than a__j), '=' (if a__i is equal to a__j) and finally '>' (if a__i is greater than a__j).
It's known that it's always possible to find both maximal and minimal elements of the array by using no more than
comparisons, where ⌈ x⌉ is the result of rounding x up.
Write the program that will find positions of the minimum and the maximum in the jury's array of length n, by using no more than f(n) comparisons.
Interaction
Each test for this problem will contain one or more arrays. You have to find positions of minimal and maximal elements for each of these arrays. The first line of the input contains integer T (1 ≤ T ≤ 1000) — number of arrays in the test.
Thus, at the beginning, you program should read number T, and then it should solve the problem for T jury's arrays one by one.
Then input for each array goes. Firstly, your program has to read the number n (1 ≤ n ≤ 50) — the length of the array. It will be provided in the next line of the input.
Further, your program can perform comparisons or report that the answer is found.
- To perform a comparison, you have to output string of the following pattern «? i j» (i and j must be integer numbers from 1 to n) — the indices of the elements to compare in the current query.
- To report the indices of minimal and maximal elements of the hidden array, your program have to output a line in the form «! i j» (i and j must be integer numbers from 1 to n), where i is an index of the minimal element of array, and j is an index of the maximal element of the array. If there are several possible answers to the problem, you can output any of them.
There are several possible responses for a comparison:
- '<' — if a__i is less than a__j,
- '=' — if a__i is equal to a__j,
- '>' — if a__i is greater than a__j.
For an array of length n your program can make at most
comparisons. Note that the operation of reporting an answer («! i j» ) is not included into the value of f(n).
After the answer is reported, your program has to solve the problem for the next array or it should terminate if all T arrays are processed.
这是一个交互式问题。每次输出一行后,你必须立即执行刷新操作。例如,在 C++ 中应使用函数 fflush(stdout),在 Java 中应使用 System.out.flush(),在 Pascal 中应使用 flush(output),而在 Python 中应使用 sys.stdout.flush()。
在本题中,你需要找出一个数组中的最大值和最小值。这难道不简单吗?
你可以想象评测系统持有一个数组,而你最初只知道一个数字 n —— 即该数组的长度。
数组元素的下标从 1 到 n 编号。你被允许通过给出两个下标 i 和 j 来比较数组中对应位置的两个元素。对该查询可能返回三种响应:<(表示 ai<aj)、=(表示 ai=aj),以及 >(表示 ai>aj)。
已知:总可以通过不超过
次比较,找出该数组的最大值与最小值,其中 ⌈x⌉ 表示对 x 向上取整。
请编写一个程序,在至多 f(n) 次比较内,找出评测系统所持长度为 n 的数组中最小值与最大值所在的位置。
交互方式
本题的每个测试用例包含一个或多个数组。你需对每个数组分别找出最小值与最大值的下标。输入的第一行是一个整数 T(1≤T≤1000),表示该测试用例中数组的个数。
因此,你的程序一开始应读入整数 T,然后依次解决 T 个由评测系统提供的数组的问题。
接下来是每个数组的输入。首先,你的程序需读入一个整数 n(1≤n≤50)——即该数组的长度。该数值将在输入的下一行中给出。
之后,你的程序可以执行比较操作,或报告答案已找到。
- 若要执行一次比较,你须输出形如 «? i j» 的字符串(其中 i 和 j 必须是 1 到 n 之间的整数),表示当前查询中要比较的两个元素的下标;
- 若要报告隐藏数组中最小值与最大值的下标,你的程序须输出形如 «! i j» 的一行(其中 i 和 j 必须是 1 到 n 之间的整数),其中 i 是数组中最小值所在位置的下标,j 是数组中最大值所在位置的下标。若存在多个合法答案,输出任意一个均可。
一次比较可能返回以下几种响应:
<—— 若 ai<aj,=—— 若 ai=aj,>—— 若 ai>aj。
对于长度为 n 的数组,你的程序最多可进行
次比较。注意:报告答案的操作(即输出 «! i j»)不计入 f(n) 的计数中。
在报告答案后,你的程序须继续处理下一个数组;若所有 T 个数组均已处理完毕,则程序应终止。
输入输出样例
输入#1
2 2 > 3 = =
输出#1
? 1 2 ! 2 1 ? 3 1 ? 2 1 ! 2 3
输入解题思路,AI测评打分。不知道怎么写?