Guess the Array
1 second
256 megabytes
standard input
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.
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 aiare 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.
5
9
7
9
11
6
? 1 5
? 2 3
? 4 1
? 5 2
? 3 4
! 4 6 1 5 5
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.
分析:交互题;
首先由a[1]+a[2],a[1]+a[3],a[2]+a[3]即可解出a[1],a[2],a[3]:
后面a[i]=(a[i]+a[i-1])-a[i-1](i>=4)即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <unordered_map>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn];
int main()
{
int i,j;
scanf("%d",&n);
printf("? 1 2\n");fflush(stdout);
scanf("%d",&m);
printf("? 1 3\n");fflush(stdout);
scanf("%d",&j);
printf("? 2 3\n");fflush(stdout);
scanf("%d",&k);
a[]=(k-j+m)/;
a[]=(k+j-m)/;
a[]=m-a[];
rep(i,,n-)
{
printf("? %d %d\n",i,i+);
fflush(stdout);
scanf("%d",&m);
a[i+]=m-a[i];
}
printf("! ");
rep(i,,n)printf("%d ",a[i]);
fflush(stdout);
//system("Pause");
return ;
}
Guess the Array的更多相关文章
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
- ES5对Array增强的9个API
为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...
- JavaScript Array对象
介绍Js的Array 数组对象. 目录 1. 介绍:介绍 Array 数组对象的说明.定义方式以及属性. 2. 实例方法:介绍 Array 对象的实例方法:concat.every.filter.fo ...
- 了解PHP中的Array数组和foreach
1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组 . 2.例子:一般的数组 这里,我 ...
- 关于面试题 Array.indexof() 方法的实现及思考
这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...
- javascript之活灵活现的Array
前言 就如同标题一样,这篇文章将会灵活的运行Array对象的一些方法来实现看上去较复杂的应用. 大家都知道Array实例有这四个方法:push.pop.shift.unshift.大家也都知道 pus ...
- 5.2 Array类型的方法汇总
所有对象都具有toString(),toLocaleString(),valueOf()方法. 1.数组转化为字符串 toString(),toLocaleString() ,数组调用这些方法,则返回 ...
- OpenGL ES: Array Texture初体验
[TOC] Array Texture这个东西的意思是,一个纹理对象,可以存储不止一张图片信息,就是说是是一个数组,每个元素都是一张图片.这样免了频繁地去切换当前需要bind的纹理,而且可以节省系统资 ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
随机推荐
- c++的输入和输出流
C++编译系统提供了用于输入输出的iostream类库.iostream这个单词是由3个部 分组成的,即i-o-stream,意为输入输出流.在iostream类库中包含许多用于输入输出的 类.常用的 ...
- Spring的Resource
通过Spring Resource接口获取资源(取自http://haohaoxuexi.iteye.com/blog/2016305)目录1 Resource简介2 通过ResourceLoader ...
- 7.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。
package com.bao; public class Print1 { int x; void output() { if(x==1) { System.out.println("AB ...
- Telnet服务器和域名系统的端口号 Mac OS X
找到Telnet服务器和域名系统的端口号: lapommedeMacBook-Pro:~ lapomme$ grep telnet /etc/services telnet /udp # Telnet ...
- CentOS 7 多网卡绑定
根据官方文档Red_Hat_Enterprise_Linux-7-Networking_Guide-en-US用nmcli做起来还是相当容易的.下面把俺的步骤贴下. 1.查看目前网卡的名称和状态.#n ...
- [Mark] openvswitch megaflow
Megaflow is an OpenVSwitch (OVS) feature than enables us to install flows for which some fields are ...
- 2.4 chrome 开发者工具
开发者工具顶部有Elements.Console.Network等八个栏目.常用的有三个:Elements,用来查看需爬取字段的HTML标签信息:Console,可以检测你的JS代码:Network, ...
- bootstrap ch2清除浮动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- angular项目——小小记事本2
一,路由的规划. 需要模拟的页面有三个:all,active,conplete. 首先,写好铺垫需要的各种东西,重要的组件的引用等—— 这里我们会将index.html设为主页,将body.html加 ...
- java程序使用memcached
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 1.linux环境下安装与启动memcache: 以编译安装的方式安装.具体参看: http://wang ...