Technocup 2017 - Elimination Round 1 (Rated for Div. 2) C. Guess the Array 交互题+解n元方程组

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.


Source

Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2)


My Solution

题意:交互题+解n元方程组


交互题+解n元方程组

首先询问

? 1 2

? 2 3

? 1 3

然后 x1 + x2 == x12       1)

        x2 + x3 == x23        2)

        x3 + x1 == x13        3)


1) -  2) + 3)可得2*x1,从而分别求出 x1 = (x12 - x23 + x13) / 2;

然后令 res = x1; //res表示前一个数的值。

然后一次根据 x[i] = x[i-1][i] - res;分别求出所有的数,

并在中间过程中把答案放到一个queue里,最后再输出即可

只要n次询问即可

/*此外

交互题,是每一个cout一次fflush(stdout),而不是每次询问一个fflush(stdout)

*/

复杂度 O(n)


#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 5000 + 8;

queue<LL> que;

int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 1;
    while(T--){
    #endif // LOCAL
    //ios::sync_with_stdio(false); cin.tie(0);

    LL n, x12, x23, x13, res, xi_1i;
    cin >> n;


    cout << "? " << 1 << " " << 2 << "\n";
    fflush(stdout);
    cin >> x12;

    cout << "? " << 2 << " " << 3 << "\n";
    fflush(stdout);
    cin >> x23;

    cout << "? " << 1 << " " << 3 << "\n";
    fflush(stdout);
    cin >> x13;

    res = (x12 - x23 + x13) / 2;
    que.push(res);
    res = x12 - res;
    que.push(res);
    res = x23 - res;
    que.push(res);

    for(int i = 4; i <= n; i++){
        cout << "? " << i - 1 << " " << i << "\n";
        fflush(stdout);
        cin >> xi_1i;
        res = xi_1i - res;
        que.push(res);
    }
    cout << "!";
    fflush(stdout);
    while(!que.empty()){
        cout << " " << que.front();
        fflush(stdout);
        que.pop();
    }



    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}


  Thank you!

                                                                                                                                               ------from ProLights 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值