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 ...
随机推荐
- Ubuntu云服务器下mysql授权远程登陆
1)首先以 root 帐户登陆 MySQL(在授权之前要确保3306端口开放)2)创建远程登陆用户并授权 > grant all PRIVILEGES on discuz.* to zhan@' ...
- js数组及常用数学方法
数组方法 清空数组 1: arr.length=0; 2: arr=[]; arr.push() //往数组最后一个添加元素,会待会一个返回值,就是新的数组长度arr.uns ...
- HTML(三)选择器--复杂选择器
1.父子选择器/派生选择器 <div calss="wrapper"> <span calss="box">123</span&g ...
- .net core 在扩展中使用接口实例之IServiceProvider
在.net core 2.0中,我们使用的对象实例大多数都是通过构造函数依赖注入进来的,但那是在一般的类中使用. 如果需要在静态/扩展类中使用某些服务类的对象实例,可以使用如下方式: 1.新建一个Se ...
- list的四种遍历方式
1.手先增强for循环和iterator遍历的效果是一样的,也就说 增强for循环的内部也就是调用iteratoer实现的,但是增强for循环 有些缺点,例如不能在增强循环里动态的删除集合内容.不能获 ...
- offSet().left 与position().left的区别
offSet().left是针对整个当前文档的所说的偏移: position().left是对于父元素来说的:
- innodb mvcc,事务隔离级别,读写锁
mvcc其实和copyonwritelist的思路差不多:读不加锁,写加锁,事务提交之后释放锁,并且延伸的是,在UNdolog里面保存了几个版本,实现不同的隔离级别.如果读数据页里面最新的数据,那么就 ...
- <算法><Union Find并查集>
Intro 想象这样的应用场景:给定一些点,随着程序输入,不断地添加点之间的连通关系(边),整个图的连通关系也在变化.这时候我们如何维护整个图的连通性(即判断任意两个点之间的连通性)呢? 一个比较简单 ...
- C代码与C++代码之间的相互调用
1.showCpp.cpp #include <iostream> using namespace std; // 声明为C语言可以调用的函数 extern "C" v ...
- linux的python版本升级
可利用Linux自带下载工具wget下载,如下所示: # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.13.tgz 下载完成后 ...