【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\n”);
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\n”,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("\n? 1 2\n");
fflush(stdout);
int k1;
scanf("%d", &k1);
printf("\n? 1 3\n");
fflush(stdout);
int k2;
scanf("%d", &k2);
printf("\n? 2 3\n");
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("\n? %d %d\n", 1, i);
fflush(stdout);
scanf("%d", &a[i]);
a[i] -= a[1];
}
printf("\n");
printf("! ");
for (int i = 1; i <= n - 1; i++)
printf("%d ", a[i]);
printf("%d\n", a[n]);
return 0;
}
【44.19%】【codeforces 727C】Guess the Array的更多相关文章
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【44.64%】【codeforces 743C】Vladik and fractions
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【30.43%】【codeforces 746C】Tram
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
随机推荐
- 微信支付v2开发(5) 订单查询
本文介绍微信支付中订单查询功能的实现. 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一.订单查询 因为某一方 ...
- 微服务实战(三):深入微服务架构的进程间通信 - DockOne.io
原文:微服务实战(三):深入微服务架构的进程间通信 - DockOne.io [编者的话]这是采用微服务架构创建自己应用系列第三篇文章.第一篇介绍了微服务架构模式,和单体式模式进行了比较,并且讨论了使 ...
- SQL判断空值、nvl处理与JOIN的使用
LIKE子句会影响查询性能,所以在明确知道字符个数时,应该使用'_',而不使用'%'. 判断空值/非空值 SELECT select_list FROM table_list/view_list WH ...
- pragma pack,字节对齐
关于字节对齐 pragma pack 一. 测试代码: // packTest.cpp : Defines the entry point for the console application. / ...
- [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime
Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...
- swift 利用 Reflect(字典转模型)
1. 导入Reflect(字典转模型)框架 2. 让它继承Reflect这个类,如下代码所示: class IWUser: Reflect { /** * 用户的ID */ var idstr:N ...
- Oracle批量插入在C#中的应用
public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- VS2010制作dll
一.为什么需要dll 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,如ATL.M ...
- Latex表格制作记录
Latex表格制作记录 主要功能 合并表格的行列 长表格的使用 makecell例程借鉴 效果图 参考代码 \documentclass{ctexart} \usepackage{indentfirs ...