zoukankan      html  css  js  c++  java
  • Guess the Array

    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 "aa2 ... 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 «aa2 ... 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.

    分析:交互题;
       首先由a[1]+a[2],a[1]+a[3],a[2]+a[3]即可解出a[1],a[2],a[3]:
       后面a[i]=(a[i]+a[i-1])-a[i-1](i>=4)即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <unordered_map>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<ll,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn];
    int main()
    {
        int i,j;
        scanf("%d",&n);
        printf("? 1 2
    ");fflush(stdout);
        scanf("%d",&m);
        printf("? 1 3
    ");fflush(stdout);
        scanf("%d",&j);
        printf("? 2 3
    ");fflush(stdout);
        scanf("%d",&k);
        a[2]=(k-j+m)/2;
        a[3]=(k+j-m)/2;
        a[1]=m-a[2];
        rep(i,3,n-1)
        {
            printf("? %d %d
    ",i,i+1);
            fflush(stdout);
            scanf("%d",&m);
            a[i+1]=m-a[i];
        }
        printf("! ");
        rep(i,1,n)printf("%d ",a[i]);
        fflush(stdout);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    java8 新特性
    class类加载机制
    redis和memcache的区别
    Nginx,Apache,Tomcat区别
    java synchronized锁的理解
    java BIO,NIO在单服务器,多客户端通信上的应用
    理解spring任务调度timer,Quartz,spring task
    理解同步与异步,及java中实现同步机制的方法
    java范型的理解
    正向代理,反向代理的理解
  • 原文地址:https://www.cnblogs.com/dyzll/p/5965690.html
Copyright © 2011-2022 走看看