B - Equalize Prices
题目链接:http://codeforces.com/contest/1183/problem/B
题目:
There are n products in the shop. The price of the i-th product is . 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.
Input
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.
Output
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.
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.
Input
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.
Output
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.
题意:给出两个数,n,k,和一个数组,长度为n,要求输出一个最大满足条件的正整数使得该数和数组元素之差的绝对值小于等于k,不满足输出-1
思路:找规律题,应当一眼找到规律,找到数组最小值,加上k,则遍历数组中每个元素,判断之差是否小于k,若有大于的,则不存在,无则输出最小值与k的和即可。
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int T; while(cin>>T) { while (T--) { int t; ll a; cin >> t >> a; int x[200]; for (int i = 0; i < t; i++) { cin >> x[i]; } ll mm = *min_element(x, x + t); int bu = mm + a; bool flag = 0; for (int i = 0; i < t; i++) { if (abs(bu - x[i])> a) { flag = 1; break; } } if (flag) cout << "-1" << endl; else cout << bu << endl; } } return 0; }