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的更多相关文章

  1. Codeforces 727C Guess the Array

    题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...

  2. 【44.19%】【codeforces 727C】Guess the Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Zookeeper的实际应用

    Zookeeper是hadoop的一个子项目,虽然源自hadoop,但是我发现zookeeper脱离hadoop的范畴开发分布式框架的运用越来越多.今天我想谈谈zookeeper,本文不谈如何使用zo ...

  2. laravel上传文件FTP驱动配置

    FTP驱动配置 Laravel 的文件系统集成了 FTP 操作,不过,框架默认的配置文件 filesystems.php 并没有提供示例配置.如果你需要配置一个FTP文件系统,可以使用以下示例配置: ...

  3. export及export default

    const a=2; const b=3; const c=function(){console.log(a+b} export a; export b; export default c; 如上文件 ...

  4. java⑿

    1.插入: 插入算法: 前提是数组中的数据必须是有序的 public static void main(String[] args) { // 先定义一个int类型的数组 int[] nums = n ...

  5. libiconv库的安装和使用

    1.libiconv包的下载路径 包的下载页面http://www.gnu.org/software/libiconv/ 2.编译安装libiconv库 $tar zxvf libiconv-1.14 ...

  6. svn服务器搭建及使用(一)

    这里郑重感谢分享作者的辛苦:http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html Subversion是优秀的版本控制工 ...

  7. Observer,观察者模式,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  8. Remove duplicates from array II

    //Given a sorted array, remove the duplicates in place such that each element appear only // once an ...

  9. SQL-25 获取员工其当前的薪水比其manager当前薪水还高的相关信息

    题目描述 获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01',结果第一列给出员工的emp_no,第二列给出其manager的manager_ ...

  10. Ajax 以及 前端JSP页面如何查看数值

    $.ajax({ url: ctx + "/unit/rsdl/qyjy/getDljgCode", type: "post", success: functi ...