zoukankan      html  css  js  c++  java
  • 【44.19%】【codeforces 727C】Guess the Array

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    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 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 ai are 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.

    【题解】

    交互题;
    printf(“? i j ”);
    fflush(stdout);
    然后scanf(“%d”,&d);
    就能把系统给你的东西输入到d这个变量里了。
    让你猜一个序列a[1..n]。
    每次你可以询问任意两个数字的和。
    最多使用n次询问,求出所有的序列;
    先弄出前3个
    询问
    1 2
    1 3
    2 3
    然后
    a1+a2=k1;
    a1+a3=k2;
    a2+a3=b;

    a3 = (b+k2-k1)/2
    a2 = (b+k1-k2)/2;(路人甲)->其实a2=b-a3更简单,(我,一个独裁者)->滚;
    a1 = k1-a2;
    然后咱们就用3次询问得到a[1..3]了;
    然后for(int i = 4;i <= n;i++)
    printf(“? 1 %d ”,i);
    询问 1 和(4..n);
    然后减去a1就是a[4..n]了;
    刚好用完n次询问;
    那个询问里面好像要输出空行,不然会出现的错误(Idleness limit exceeded on pretest 1);当然不知道是不是这个原因,反正输出空行的地方按照样例的输出的输出吧

    #include <cstdio>
    
    const int MAXN = 6000;
    
    int n,a1;
    int a[MAXN];
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        scanf("%d", &n);
        printf("
    ? 1 2
    ");
        fflush(stdout);
        int k1;
        scanf("%d", &k1);
        printf("
    ? 1 3
    ");
        fflush(stdout);
        int k2;
        scanf("%d", &k2);
        printf("
    ? 2 3
    ");
        fflush(stdout);
        int b;
        scanf("%d", &b);
        a[2] = (b + k1 - k2) / 2;
        a[3] = (b + k2 - k1) / 2;
        a[1] = k1 - a[2];
        for (int i = 4; i <= n; i++)
        {
            printf("
    ? %d %d
    ", 1, i);
            fflush(stdout);
            scanf("%d", &a[i]);
            a[i] -= a[1];
        }
        printf("
    ");
        printf("! ");
        for (int i = 1; i <= n - 1; i++)
            printf("%d ", a[i]);
        printf("%d
    ", a[n]);
        return 0;
    }
  • 相关阅读:
    牛客网-《剑指offer》-包含min函数的栈
    牛客网-《剑指offer》-调整数组顺序使奇数位于偶数前面
    Gate Decorator: Global Filter Pruning Method for Accelerating Deep Convolutional Neural Networks
    pytorch数据预处理错误
    python2 pickle.dump生成的文件,python3 pickle.load怎么加载
    CSAGAN的几大重点
    BicycleGAN: Toward Multimodal Image-to-Image Translation
    StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation
    MUNIT:Multimodal Unsupervised Image-to-Image Translation
    SAGAN:Self-Attention Generative Adversarial Networks
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632149.html
Copyright © 2011-2022 走看看