【codeforces 727 C】【交互题 求原数组】【告诉你有一个长度为n序列,你可以问n个问题,每个问题为ai+aj等于多少,最后输出这个序列】

本文介绍了一个互动问题解决方法,目标是在未知序列的情况下通过询问元素之和来逐步确定整个序列。文章详细阐述了算法思路及其实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:C. Guess the Array

描述:

C. Guess the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

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.

题意:

给你一个序列,长度为n,你事先不知道这个序列是什么,你可以问n个问题,每个问题为ai+aj等于多少,最后输出这个序列

思路:

 先问3个问题算出前3个,后面直接推就好了,然后就是注意交互题的格式
代码:
#include <bits/stdc++.h>
#define rep(i,k,n) for(int i=k;i<=n;i++)
using  namespace  std;

const int N=5010;

int a[N], n;

int ask(int i, int j){
  int res;
  cout<<"? "<<i<<' '<<j<<endl;
  fflush(stdout);
  cin>>res;
  return res;
}

int  main(){
  cin>>n;
  int x = ask(1, 2);
  int y = ask(1, 3);
  int z = ask(2, 3);
  a[1] = (x + y - z) / 2;
  a[2] = x - a[1];
  a[3] = y - a[1];
  rep(i, 3, n-1){
    int res = ask(i, i+1);
    a[i + 1] = res - a[i];
  }
  cout<<"! ";
  rep(i, 1, n){
    cout<<a[i]<<' ';
  }
  cout<<endl;
  return 0;
}

/*5
10
5
7
6
10*/




Codeforces Div.3比赛中,实现高效的排序算法对于解决数组元素去重问题至关重要。在众多排序算法中,快速排序因其平均情况下的时间复杂度为O(n log n),在数据随机分布时表现出色而受到推崇。下面详细说明如何使用快速排序算法解决该问题,以帮助你在Codeforces比赛或其他编程竞赛中脱颖而出。 参考资源链接:[Codeforces Round 962(Div.3) AI笔记解析](https://wenku.csdn.net/doc/3ajympq7g1?spm=1055.2569.3001.10343) 快速排序算法的基本思想是分治策略。首先选择一个基准值(pivot),然后将数组分成三部分:小于基准值的元素、等于基准值的元素和大于基准值的元素。这个过程称为分区(partitioning)。递归地对小于和大于基准值的子数组进行快速排序,最后合并结果。 以下是一个C语言实现快速排序的示例代码,包含了数组去重的功能: ```c #include <stdio.h> // 交换两个元素的值 void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // 分区函数 int partition(int arr[], int low, int high) { int pivot = arr[high]; // 选择最后一个元素作为基准 int i = (low - 1); for (int j = low; j <= high - 1; j++) { // 如果当前元素小于或等于基准 if (arr[j] <= pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } // 快速排序函数 void quickSort(int arr[], int low, int high) { if (low < high) { // pi 是分区索引,arr[pi] 现在在正确的位置 int pi = partition(arr, low, high); // 分别递归地对分区前后的元素进行排序 quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } // 检查并去除数组中的重复元素 void removeDuplicates(int arr[], int n) { int temp[n]; int j = 0; // 将数组中的元素复制到临时数组中,同时去除重复元素 for (int i = 0; i < n; i++) { if (i == 0 || arr[i] != arr[i - 1]) { temp[j++] = arr[i]; } } // 将去除重复元素后的数组复制回数组 for (int i = 0; i < j; i++) { arr[i] = temp[i]; } } // 打印数组元素 void printArray(int arr[], int size) { for (int i = 0; i < size; i++) printf( 参考资源链接:[Codeforces Round 962(Div.3) AI笔记解析](https://wenku.csdn.net/doc/3ajympq7g1?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值