C. Guess the Array(codeforces)
2016-10-15 21:49
369 查看
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 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 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
output
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个数是第几个数加上第几个数。我们这里假设前n-1个数是第i+1和第一个数相加的结果。然后最后一个数是第二个和第三个数相加的结果。然后求出第一个数然后再用两数和减去第一个数就求出了数组。
代码:
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 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 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个数是第几个数加上第几个数。我们这里假设前n-1个数是第i+1和第一个数相加的结果。然后最后一个数是第二个和第三个数相加的结果。然后求出第一个数然后再用两数和减去第一个数就求出了数组。
代码:
#include<stdio.h> #include<cstring> #include<iostream> #include<algorithm> #include<math.h> #include<stdlib.h> #include<stack> #include<vector> #include<string.h> #include<map> #define INF 0x3f3f3f3f3f using namespace std; int main() { int n,a23; scanf("%d",&n); int a[n+1]; for(int i=1;i<n;i++) { printf("? 1 %d\n",i+1); fflush(stdout);//在每个输出的后面都要加上,否则会出现Idleness limit exceeded on test 1 scanf("%d",&a[i]); } printf("? 2 3\n"); fflush(stdout); scanf("%d",&a23); int a1=(a[1]+a[2]-a23)/2; printf("! %d",a1); fflush(stdout); for(int i=1;i<n;i++) { printf(" %d",a[i]-a1); fflush(stdout); } printf("\n"); fflush(stdout); }
相关文章推荐
- [Codeforces 727C] Guess The Array 交互题
- 【44.19%】【codeforces 727C】Guess the Array
- CodeForces 727C GuessTheArray 第一道交互题
- Technocup 2017 - Elimination Round 1 (Rated for Div. 2) C. Guess the Array 交互题+解n元方程组
- Technocup 2017 - Elimination Round 1 C. Guess the Array (交互题)
- Codeforces Div. 2 #258-B. Sort the Array
- codeforces 949B A Leapfrog in the Array
- Codeforces 618B - Guess the Permutation
- 【CodeForces 618B】Guess the Permutation
- codeforces 558B B. Amr and The Large Array(水题)
- Codeforces 949B A Leapfrog in the Array(数学,规律)
- CodeForces 618 B. Guess the Permutation(水~)
- Codeforces--949B--A Leapfrog in the Array
- CodeForces-1007A Reorder the Array 贪心 田忌赛马
- Codeforces 429C Guess the Tree(状压DP+贪心)
- codeforces- 949B - A Leapfrog in the Array(思维)
- 【CodeForces 618B 】Guess the Permutation(水题)
- Codeforces 950D A Leapfrog in the Array
- [codeforces Technocup 2017 727C]Guess the Array
- codeforces 949B :A Leapfrog in the Array