【codeforces 727 C】【交互题 求原数组】【告诉你有一个长度为n序列,你可以问n个问题,每个问题为ai+aj等于多少,最后输出这个序列】
2016-10-15 21:11
1396 查看
传送门:C. Guess the Array
描述:
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 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
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个问题,每个问题为ai+aj等于多少,最后输出这个序列
思路:
先问3个问题算出前3个,后面直接推就好了,然后就是注意交互题的格式
代码:
描述:
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 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.
题意:
给你一个序列,长度为n,你事先不知道这个序列是什么,你可以问n个问题,每个问题为ai+aj等于多少,最后输出这个序列
思路:
先问3个问题算出前3个,后面直接推就好了,然后就是注意交互题的格式
代码:
#include <bits/stdc++.h> #define rep(i,k,n) for(int i=k;i<=n;i++) using namespace std; const int N=5010; int a , n; int ask(int i, int j){ int res; cout<<"? "<<i<<' '<<j<<endl; fflush(stdout); cin>>res; return res; } int main(){ cin>>n; int x = ask(1, 2); int y = ask(1, 3); int z = ask(2, 3); a[1] = (x + y - z) / 2; a[2] = x - a[1]; a[3] = y - a[1]; rep(i, 3, n-1){ int res = ask(i, i+1); a[i + 1] = res - a[i]; } cout<<"! "; rep(i, 1, n){ cout<<a[i]<<' '; } cout<<endl; return 0; } /*5 10 5 7 6 10*/
相关文章推荐
- 二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。 对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为: ( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根 小易想知道最多可以放多少块蛋糕在网格盒子里。 输入描述: 每组数组包含网格长宽W,
- 给定一个数列a1,a2,a3,...,an和m个三元组表示的查询,对于每个查询(i,j,k),输出ai,ai+1,...,aj的升序排列中第k个数。
- 【1】 设一个长度为10的整型数组,  0)要求每个元素的值通过scanf输入,输入完成后,  1)请顺序输出这些整数,  2)请倒序输出这些整数,  3)输出这些数中的最大值,最小值
- 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
- Problem Description 求n个数的最小公倍数。 Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。 Output 为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。 Sample
- 问题: 一个数组长度为n, 求出其最小的k个元素并从小到大输出
- 编写一个JAVA程序,创建指定长度的 int 型数组,并生成 100 以内随机数为数组中的每个元素赋值,然后输出数组
- 给定一个数列a1,a2,a3,...,an和m个三元组表示的查询,对于每个查询(i,j,k),输出ai,ai+1,...,aj的升序排列中第k个数。
- 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在
- 实现一个"可变二维数组",这个二维数组的行数要求用户从键盘输入,每行的元素个数仍由用户从键盘输入. //数组元素值用户可以自定义.最后把二维数组打印出来.不要忘记释放数组内存. /*void main
- Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。 Input 输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义
- 给定一个固定长度的数组,将递增整数序列写入这个数组。当写到数组尾部时,返回数组开始重新写,并覆盖先前写过的数,请在这个特殊数组中找出给定的整数
- 设一个长度为10的整型数组, 0)要求每个元素的值通过scanf输入,输入完成后, 1)请顺序输出这些整数, 2)请倒序输出这些整数, 3)输出这些数中的最大值, 4)输出这些数中的最小值
- 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。
- 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 第五题:n 只奶牛坐在一排,每个奶牛拥有 ai 个苹果,现在你要在它们之间转移苹果,使得最后所有奶牛拥有的苹果数都相同,每一次,你只能从一只奶牛身上拿走恰好两个苹果到另一个奶牛上,问最少需要移动多少次可以平分苹果,如果方案不存在输出 -1
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
- 不使用loop循环,创建一个长度为100的数组,并且每个元素的值等于它的下标
- 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。