Technocup 2017-elimination Round 1 (Rated for DIV. 2) C. Guess the Array interaction problem + solution N-ary equations

Source: Internet
Author: User
Tags flush time limit

C. Guess the Array time limit per test 1 second memory limit per test megabytes input standard input output standard o Utput

This was an interactive problem. You should use the 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 the guess an array a which are unknown for you. The only information you has initially is the length n of the Arraya.

The only allowed action was to ask the sum of the elements by their indices. Formally, you can print II indices i and J (the indices should be distinct). Then your program should read the response:the a single integer equals to Ai + AJ.

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

Write a program, that would 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 this your program should print to the standard output the requests about the sum of the elements or inform the Array is guessed. In case your program was making a request to ask the sum of the elements, it should print line in the format? I J "(I and J are distinct integers between 1 and N), where I and j is indices in the array A. In case your program informs. The array is guessed, it should print line in the format "! A1 A2 ... an "(it's guaranteed that all aiare positive integers not exceeding), where AI is the i-th element of the A Rray A.

The response on a request was a single integer equal to Ai + AJ, printed on a separate line.

Your program can does at the most n requests. Note that the final line«! A1 A2 ... an»is not counted as a request.

Don't forget about flush operation after each printed line.

After your program prints the guessed array, it should terminate normally. Example input

5
9
7
9
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 arr Ay. 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

Test instructions: Interaction problem + solution N-ary equations


Interaction problem + solving n-ary equations

First Ask

? 1 2

? 2 3

? 1 3

Then x1 + x2 = = x12 1)

x2 + x3 = = x23 2)

x3 + X1 = = x13 3)


1)-2) + 3) 2*x1 can be obtained, thus x1 = (x12-x23 + x13)/2 respectively;

Then make res = x1; Res represents the value of the previous number.

Then one time according to x[i] = X[i-1][i]-res, each to find out all the numbers,

And in the middle of the process to put the answer in a queue, and finally output can

Just ask for n times.

/* In addition

Interactive problem, is every cout once Fflush (stdout), not every time asked a fflush (stdout)

*/

Complexity 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.