CF1183B.Equalize Prices
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n products in the shop. The price of the i-th product is ai. 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 i in such a way that the difference between the old price of this product ai and the new price bi is at most k. In other words, the condition ∣ai−bi∣≤k should be satisfied (∣x∣ is the absolute value of x).
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 bi of each product i should be positive (i.e. bi>0 should be satisfied for all i from 1 to n).
Your task is to find out the maximum possible equal price B of all productts with the restriction that for all products the condiion ∣ai−B∣≤k should be satisfied (where ai is the old price of the product and B is the same new price of all products) or report that it is impossible to find such price B.
Note that the chosen price B should be integer.
You should answer q independent queries.
商店中有 n 种商品,第 i 种商品的价格为 ai。店主希望将所有商品的价格调整为相同值,但要求价格调整过程平缓。
具体而言,店主可以对第 i 种商品的价格进行一次修改,使其新价格 bi 与原价格 ai 的差的绝对值不超过 k,即需满足条件 ∣ai−bi∣≤k(其中 ∣x∣ 表示 x 的绝对值)。
每种商品最多只能修改一次价格(当然也可以不修改,即保留原价)。此外,每种商品的新价格 bi 必须为正数(即对所有 i=1,2,…,n,均需满足 bi>0)。
你的任务是:在满足对所有商品均有 ∣ai−B∣≤k 的前提下(其中 ai 是第 i 种商品的原价,B 是所有商品统一的新价格),找出可能的最大整数 B;若不存在满足条件的 B,则报告该情况不可行。
注意:所选价格 B 必须为整数。
你需要回答 q 个相互独立的查询。
输入格式
The first line of the input contains one integer q (1≤q≤100) — the number of queries. Each query is presented by two lines.
The first line of the query contains two integers n and k (1≤n≤100,1≤k≤108) — the number of products and the value k. The second line of the query contains n integers a1,a2,…,an (1≤ai≤108), where ai is the price of the i-th product.
输入的第一行包含一个整数 q(1≤q≤100),表示查询次数。每次查询由两行组成。
查询的第一行包含两个整数 n 和 k(1≤n≤100,1≤k≤108),分别表示商品数量和数值 k。查询的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤108),其中 ai 表示第 i 个商品的价格。
输出格式
Print q integers, where the i-th integer is the answer B on the i-th query.
If it is impossible to equalize prices of all given products with restriction that for all products the condition ∣ai−B∣≤k should be satisfied (where ai is the old price of the product and B is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.
输出 q 个整数,其中第 i 个整数为第 i 次查询的答案 B。
如果无法在满足对所有商品均有 ∣ai−B∣≤k(其中 ai 是商品的原价格,B 是所有商品统一调整后的新价格)这一限制条件下使所有给定商品的价格相等,则输出 -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=2. It is easy to see that the difference between each old price and each new price B=2 is no more than 1.
In the second example query you can choose the price B=6 and then all the differences between old and new price B=6 will be no more than 2.
In the third example query you cannot choose any suitable price B. For any value B at least one condition out of two will be violated: ∣1−B∣≤2, ∣6−B∣≤2.
In the fourth example query all values B between 1 and 7 are valid. But the maximum is 7, so it's the answer.
在第一个示例查询中,你可以选择价格 B=2。容易看出,每个原始价格与新价格 B=2 之间的差值均不超过 1。
在第二个示例查询中,你可以选择价格 B=6,此时所有原始价格与新价格 B=6 之间的差值均不超过 2。
在第三个示例查询中,你无法选择任何合适的价格 B。对于任意值 B,以下两个条件中至少有一个不成立:∣1−B∣≤2,∣6−B∣≤2。
在第四个示例查询中,所有介于 1 和 7 之间的值 B 均有效。但其中最大值为 7,因此答案是 7。
输入解题思路,AI测评打分。不知道怎么写?