CF1707B.Difference Array

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa consisting of nn non-negative integers. It is guaranteed that aa is sorted from small to large.

For each operation, we generate a new array bi=ai+1−aib_i=a_{i+1}-a_{i} for 1≤i<n1 \le i \lt n. Then we sort bb from small to large, replace aa with bb, and decrease nn by 11.

After performing n−1n-1 operations, nn becomes 11. You need to output the only integer in array aa (that is to say, you need to output a1a_1).

给你一个由 nn 个非负整数组成的数组 aa。保证 aa 是从小到大排序的。

每次操作中,我们生成一个新数组 bb,其中 bi=ai+1−aib_i = a_{i+1} - a_i(1≤i<n1 \le i < n)。然后将 bb 从小到大排序,用 bb 替换 aa,并将 nn 减 11。

执行 n−1n-1 次操作后,nn 变为 11。你需要输出此时数组 aa 中唯一的整数(即输出 a1a_1)。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer nn (2≤n≤1052\le n\le 10^5) — the length of the array aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\dots,a_n (0≤a1≤…≤an≤5⋅1050\le a_1\le \ldots\le a_n \le 5\cdot 10^5) — the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2.5⋅1052.5\cdot 10^5, and the sum of ana_n over all test cases does not exceed 5⋅1055\cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052\le n\le 10^5),表示数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\dots,a_n(0≤a1≤…≤an≤5⋅1050\le a_1\le \ldots\le a_n \le 5\cdot 10^5),即数组 aa。

保证所有测试用例的 nn 之和不超过 2.5⋅1052.5\cdot 10^5,且所有测试用例的 ana_n 之和不超过 5⋅1055\cdot 10^5。

输出格式

For each test case, output the answer on a new line.

对于每个测试用例,在新的一行输出答案。

输入输出样例

  • 输入#1

    5
    3
    1 10 100
    4
    4 8 9 13
    5
    0 0 0 8 13
    6
    2 4 8 16 32 64
    7
    0 0 0 0 0 0 0

    输出#1

    81
    3
    1
    2
    0

说明/提示

To simplify the notes, let sort⁡(a)\operatorname{sort}(a) denote the array you get by sorting aa from small to large.

In the first test case, a=[1,10,100]a=[1,10,100] at first. After the first operation, a=sort⁡([10−1,100−10])=[9,90]a=\operatorname{sort}([10-1,100-10])=[9,90]. After the second operation, a=sort⁡([90−9])=[81]a=\operatorname{sort}([90-9])=[81].

In the second test case, a=[4,8,9,13]a=[4,8,9,13] at first. After the first operation, a=sort⁡([8−4,9−8,13−9])=[1,4,4]a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]. After the second operation, a=sort⁡([4−1,4−4])=[0,3]a=\operatorname{sort}([4-1,4-4])=[0,3]. After the last operation, a=sort⁡([3−0])=[3]a=\operatorname{sort}([3-0])=[3].

为简化说明,记 sort⁡(a)\operatorname{sort}(a) 为将数组 aa 按从小到大排序后所得的数组。

在第一个测试用例中,初始时 a=[1,10,100]a=[1,10,100]。第一次操作后,a=sort⁡([10−1,100−10])=[9,90]a=\operatorname{sort}([10-1,100-10])=[9,90]。第二次操作后,a=sort⁡([90−9])=[81]a=\operatorname{sort}([90-9])=[81]。

在第二个测试用例中,初始时 a=[4,8,9,13]a=[4,8,9,13]。第一次操作后,a=sort⁡([8−4,9−8,13−9])=[1,4,4]a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]。第二次操作后,a=sort⁡([4−1,4−4])=[0,3]a=\operatorname{sort}([4-1,4-4])=[0,3]。最后一次操作后,a=sort⁡([3−0])=[3]a=\operatorname{sort}([3-0])=[3]。

输入解题思路,AI测评打分。不知道怎么写?

首页