CF727C.Guess the Array

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should use fflush(stdout), in Java you should use System.out.flush(), and in Pascal — flush(output).

In this problem you should guess an array a which is unknown for you. The only information you have initially is the length n of the array a.

The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices i and j (the indices should be distinct). Then your program should read the response: the single integer equals to a__i + a__j.

It is easy to prove that it is always possible to guess the array using at most n requests.

Write a program that will guess the array a by making at most n requests.

Interaction

In each test your program should guess a single array.

The input starts with a line containing integer n (3 ≤ n ≤ 5000) — the length of the array. Your program should read it at first.

After that your program should print to the standard output the requests about the sum of two elements or inform that the array is guessed.

  • In case your program is making a request to ask the sum of two elements, it should print line in the format "? i j" (i and j are distinct integers between 1 and n), where i and j are indices in the array a.
  • In case your program informs that the array is guessed, it should print line in the format "! _a_1 _a_2 ... a__n" (it is guaranteed that all a__i are positive integers not exceeding 105), where a__i is the i-th element of the array a.

The response on a request is a single integer equal to a__i + a__j, printed on a separate line.

Your program can do at most n requests. Note that the final line «! _a_1 _a_2 ... a__n» is not counted as a request.

Do not forget about flush operation after each printed line.

After you program prints the guessed array, it should terminate normally.

这是一个交互式问题。每次输出一行后,你都需要执行刷新操作(flush)。例如,在 C++ 中应使用 fflush(stdout),在 Java 中应使用 System.out.flush(),而在 Pascal 中应使用 flush(output)。

本题中,你需要猜测一个对你而言未知的数组 aa。你最初唯一已知的信息是该数组 aa 的长度 nn。

唯一允许的操作是通过下标询问两个元素的和。形式化地说,你可以输出两个下标 ii 和 jj(这两个下标必须不同),然后你的程序应读入一个响应:一个整数,其值等于 ai+aja_i + a_j。

可以证明,总是存在一种策略,仅用至多 nn 次询问即可完全确定该数组。

请编写一个程序,通过至多 nn 次询问来猜出数组 aa。

交互方式

在每个测试用例中,你的程序需要猜出一个单独的数组。

输入的第一行是一个整数 nn(3≤n≤50003 \leq n \leq 5000)——即数组的长度。你的程序应首先读入该值。

此后,你的程序应向标准输出打印询问(请求两个元素之和)或宣布已猜出整个数组。

  • 若你的程序要请求两个元素之和,则应输出格式为 "? i j" 的一行(其中 ii 和 jj 是 11 到 nn 之间互不相等的整数),表示请求 ai+aja_i + a_j;
  • 若你的程序宣布已猜出数组,则应输出格式为 "! a_1 a_2 ... a_n" 的一行(保证所有 aia_i 均为不超过 10510^5 的正整数),其中 aia_i 表示数组 aa 的第 ii 个元素。

对每次询问的响应是一个单独的整数,其值为 ai+aja_i + a_j,将单独占一行输出。

你的程序最多可进行 nn 次询问。注意,最后输出的 "! a_1 a_2 ... a_n" 不计入询问次数。

每次输出一行后,请勿忘记执行刷新操作(flush)。

在你的程序输出所猜出的数组后,应正常终止。

输入输出样例

  • 输入#1

    5
    
    9
    
    7
    
    9
    
    11
    
    6
     

    输出#1

     
    ? 1 5
    
    ? 2 3
    
    ? 4 1
    
    ? 5 2
    
    ? 3 4
    
    ! 4 6 1 5 5

说明/提示

The format of a test to make a hack is:

  • The first line contains an integer number n (3 ≤ n ≤ 5000) — the length of the array.
  • The second line contains n numbers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 105) — the elements of the array to guess.

构造一个用于“hack”的测试用例的格式如下:

  • 第一行包含一个整数 $ n (( 3 \leq n \leq 5000 $)—— 表示数组的长度。
  • 第二行包含 $ n $ 个数 $ a_1,,a_2,,\dots,,a_n (( 1 \leq a_i \leq 10^5 $)—— 表示待猜测的数组元素。

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

首页