[Codeforces 727C] Guess The Array 交互题

来源:互联网 发布:微杂志免费制作软件 编辑:程序博客网 时间:2024/05/17 06:33
C. Guess the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should usefflush(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 arraya.

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 ai + aj.

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 "a1 a2 ... an" (it is guaranteed that all aiare positive integers not exceeding 105), where ai is the i-th element of the array a.

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

Your program can do at most n requests. Note that the final line «a1 a2 ... an» 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.

Example
input
5 9 7 9 11 6 
output
 ? 1 5 ? 2 3 ? 4 1 ? 5 2 ? 3 4 ! 4 6 1 5 5
Note

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 a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array to guess.


大致题意:给定N,系统生成一个长度为N的数列,数列的每一项不超过10^5,每次询问数列中某两项之和,N次询问求出这个数列,N<=5000.
题解:首先,每次询问相当于得到了一个方程,在本题中相当于N个方程解N个未知数,一定是有解的,
          但是,如果直接用高斯消元法解方程,时间复杂度是O(N^3)的,会TLE。
           考虑得到的方程的性质,考虑类似于迭代地去求解,
           先3次询问A1+A2,A2+A3,A1+A3,容易得到A1,A2,A3的具体值,
           然后对于k>=4,每次询问A1+Ak即可得到Ak的值,总共N次询问,每次询问O(1),时间复杂度为O(N).
Code:
#include <bits/stdc++.h>using namespace std;int a[5005],n;int main (){int i,a1,a2,a3;scanf ("%d",&n);printf ("? 1 2\n");fflush(stdout);scanf ("%d",&a1);printf ("? 2 3\n");fflush(stdout);scanf ("%d",&a2);printf ("? 1 3\n");fflush(stdout);scanf ("%d",&a3);a[1]=(a1+a2+a3-2*a2)/2;a[2]=(a1+a2+a3-2*a3)/2;a[3]=(a1+a2+a3-2*a1)/2;for (i=4;i<=n;i++){printf ("? 1 %d\n",i);fflush(stdout);scanf ("%d",&a1);a[i]=a1-a[1];}printf ("! ");for (i=1;i<=n;i++){printf ("%d ",a[i]);}return 0;}


原创粉丝点击