CF1183B.Equalize Prices

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn products in the shop. The price of the ii-th product is aia_i. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.

In fact, the owner of the shop can change the price of some product ii in such a way that the difference between the old price of this product aia_i and the new price bib_i is at most kk. In other words, the condition ∣ai−bi∣≤k|a_i - b_i| \le k should be satisfied (∣x∣|x| is the absolute value of xx).

He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price bib_i of each product ii should be positive (i.e. bi>0b_i \gt 0 should be satisfied for all ii from 11 to nn).

Your task is to find out the maximum possible equal price BB of all productts with the restriction that for all products the condiion ∣ai−B∣≤k|a_i - B| \le k should be satisfied (where aia_i is the old price of the product and BB is the same new price of all products) or report that it is impossible to find such price BB.

Note that the chosen price BB should be integer.

You should answer qq independent queries.

商店中有 nn 种商品,第 ii 种商品的价格为 aia_i。店主希望将所有商品的价格调整为相同值,但要求价格调整过程平缓。

具体而言,店主可以对第 ii 种商品的价格进行一次修改,使其新价格 bib_i 与原价格 aia_i 的差的绝对值不超过 kk,即需满足条件 ∣ai−bi∣≤k|a_i - b_i| \le k(其中 ∣x∣|x| 表示 xx 的绝对值)。

每种商品最多只能修改一次价格(当然也可以不修改,即保留原价)。此外,每种商品的新价格 bib_i 必须为正数(即对所有 i=1,2,…,ni = 1, 2, \dots, n,均需满足 bi>0b_i > 0)。

你的任务是:在满足对所有商品均有 ∣ai−B∣≤k|a_i - B| \le k 的前提下(其中 aia_i 是第 ii 种商品的原价,BB 是所有商品统一的新价格),找出可能的最大整数 BB;若不存在满足条件的 BB,则报告该情况不可行。

注意:所选价格 BB 必须为整数。

你需要回答 qq 个相互独立的查询。

输入格式

The first line of the input contains one integer qq (1≤q≤1001 \le q \le 100) — the number of queries. Each query is presented by two lines.

The first line of the query contains two integers nn and kk (1≤n≤100,1≤k≤1081 \le n \le 100, 1 \le k \le 10^8) — the number of products and the value kk. The second line of the query contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1081 \le a_i \le 10^8), where aia_i is the price of the ii-th product.

输入的第一行包含一个整数 qq(1≤q≤1001 \le q \le 100),表示查询次数。每次查询由两行组成。

查询的第一行包含两个整数 nn 和 kk(1≤n≤1001 \le n \le 100,1≤k≤1081 \le k \le 10^8),分别表示商品数量和数值 kk。查询的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1081 \le a_i \le 10^8),其中 aia_i 表示第 ii 个商品的价格。

输出格式

Print qq integers, where the ii-th integer is the answer BB on the ii-th query.

If it is impossible to equalize prices of all given products with restriction that for all products the condition ∣ai−B∣≤k|a_i - B| \le k should be satisfied (where aia_i is the old price of the product and BB is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.

输出 qq 个整数,其中第 ii 个整数为第 ii 次查询的答案 BB。

如果无法在满足对所有商品均有 ∣ai−B∣≤k|a_i - B| \le k(其中 aia_i 是商品的原价格,BB 是所有商品统一调整后的新价格)这一限制条件下使所有给定商品的价格相等,则输出 -1;否则输出所有商品可能达到的最大统一价格。

输入输出样例

  • 输入#1

    4
    5 1
    1 1 2 3 1
    4 2
    6 4 8 5
    2 2
    1 6
    3 5
    5 2 5

    输出#1

    2
    6
    -1
    7

说明/提示

In the first example query you can choose the price B=2B=2. It is easy to see that the difference between each old price and each new price B=2B=2 is no more than 11.

In the second example query you can choose the price B=6B=6 and then all the differences between old and new price B=6B=6 will be no more than 22.

In the third example query you cannot choose any suitable price BB. For any value BB at least one condition out of two will be violated: ∣1−B∣≤2|1-B| \le 2, ∣6−B∣≤2|6-B| \le 2.

In the fourth example query all values BB between 11 and 77 are valid. But the maximum is 77, so it's the answer.

在第一个示例查询中,你可以选择价格 B=2B=2。容易看出,每个原始价格与新价格 B=2B=2 之间的差值均不超过 11。

在第二个示例查询中,你可以选择价格 B=6B=6,此时所有原始价格与新价格 B=6B=6 之间的差值均不超过 22。

在第三个示例查询中,你无法选择任何合适的价格 BB。对于任意值 BB,以下两个条件中至少有一个不成立:∣1−B∣≤2|1-B| \le 2,∣6−B∣≤2|6-B| \le 2。

在第四个示例查询中,所有介于 11 和 77 之间的值 BB 均有效。但其中最大值为 77,因此答案是 77。

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

首页