CodeForces 727C
zsy:
Guess the Array
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 727C
Description
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.
Sample Input
Input
5
9
7
9
11
6
Output
? 1 5
? 2 3
? 4 1
? 5 2
? 3 4
! 4 6 1 5 5
Hint
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。输出? x y获取x+y,只能问n次。数列长度>=3
思路:首先确定数列前三个数(a,b,c):获取a+b;a+c;b+c。通过解方程组可以确定a,b,c;
之后第四个数,可以通过获取第三加第四个数的和来确定;以此类推。
AC代码:
//#define LOCAL
#include <stdio.h>
#include <stdlib.h>
int main(){
int *a,n;
int b[3];
int i;
#ifdef LOCAL
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n); //由于数据量未知,因此用动态内存分配
printf("\n? 1 2\n");
fflush(stdout); //题目中要求在每个printf后加此代码
scanf("%d",&b[0]);
printf("\n? 1 3\n");
fflush(stdout);
scanf("%d",&b[1]);
printf("\n? 2 3\n");
fflush(stdout);
scanf("%d",&b[2]);
a[0]=(b[0]+b[1]-b[2])/2;
a[1]=(b[0]-b[1]+b[2])/2;
a[2]=(-b[0]+b[1]+b[2])/2;
for(i=3;i<n;i++){
int s;
printf("\n? %d %d\n",i,i+1);
fflush(stdout);
scanf("%d",&s);
a[i]=s-a[i-1];
}
printf("\n");
printf("! ");
for(i=0;i<n;i++){
printf("%d",a[i]);
fflush(stdout);
if(i<n-1) printf(" ");
fflush(stdout);
}
free(a); //释放内存
return 0;
}
CodeForces 727C的更多相关文章
- Codeforces 727C Guess the Array
题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...
- 【44.19%】【codeforces 727C】Guess the Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- oracle 如何查看当前用户的表空间名称
如何查询当前用户的表空间名称?因为oracle建立索引,需要知道当前用户的表空间,查找了一下资料 --查询语法-- select default_tablespace from dba_users w ...
- Node.js是用来干嘛的
如果你去年注意过技术方面的新闻,我敢说你至少看到node.js不下一两次.那么问题来了“node.js是什么?”.有些人没准会告诉你“这是一种通过JavaScript语言开发web服务端的东西”.如果 ...
- 1-2Controller之Session
laravel5.5版本. 视频教程是慕课网中的:轻松学会Laravel-表单篇 1-2 /*session简介: 1.由于HTTP协议是无状态(Stateless)的,所以session提供一种保存 ...
- Values & Reference:值和引用
var a = 2; var b = a; //b 是 a 的值的一个副本 b++; a; b; var c = [1, 2, 3]; var d = c; // d 是 值[1, 2, 3]的一个引 ...
- python-面向对象增强版
class Person: def __init__(self, name, id, gender, birth): self.name = name # 实例变量 对象里的变量 self.id = ...
- 每天CSS学习之letter-spacing
letter-spacing是CSS的一个属性,其作用是设置字符之间的距离.letter意为字符. 1.normal:规定字符之间没有额外的空间.该值是默认值.如下示例: p{ letter-spac ...
- relativeURL 相对URL的坑
我正在尝试实现一个使用RestKit的iOS应用程序.在我迄今为止看到的所有示例中,以下代码用于创建URL: NSURL *baseURL = [NSURL URLWithString:@" ...
- 3.5 C++间接继承
参考:http://www.weixueyuan.net/view/6362.html 总结: 假设类C继承自类B,类B继承自类A.那么类C中的除了能够继承B类的成员函数和成员变量外,同样也能继承B类 ...
- SharePoint Framework 企业向导(九)
博客地址:http://blog.csdn.net/FoxDave 管理SPFx解决方案的容量 所有部署到租户的SPFx解决方案必须被租户管理员审批通过.这是通过上传SPFx包(.sppkg)到A ...
- 循环神经网络-Dropout
dropout 是 regularization 方法,在rnn中使用方法不同于cnn 对于rnn的部分不进行dropout,也就是说从t-1时候的状态传递到t时刻进行计算时,这个中间不进行memor ...