There are n products in the shop. The price of the ii-th product is aiai. 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 aiai and the new price bibi is at most kk. In other words, the condition |ai−bi|≤k|ai−bi|≤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 bibiof each product ii should be positive (i.e. bi>0bi>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|ai−B|≤k should be satisfied (where aiai 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.
The first line of the input contains one integer qq (1≤q≤1001≤q≤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≤n≤100,1≤k≤108) — the number of products and the value kk. The second line of the query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1081≤ai≤108), where aiai is the price of the ii-th product.
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|ai−B|≤k should be satisfied (where aiai 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.
4 5 1 1 1 2 3 1 4 2 6 4 8 5 2 2 1 6 3 5 5 2 5
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|≤2, |6−B|≤2|6−B|≤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.
Analysis:
It is very intuitive that the maximum price we can obtain is min+kmin+k where minmin is the minimum value in the array. For this price we should check that we can change prices of all products to it. It can be done very easily: we can just check if each segment [ai−k;ai+k][ai−k;ai+k] covers the point min+kmin+k. But this is not necessary because if we can change the price of the maximum to this value (min+kmin+k) then we can change each price in the segment [min;max][min;max] to this value. So we just need to check that min+k≥max−kmin+k≥max−k and if it is then print min+kmin+k otherwise print -1.
codes:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 #ifdef _DEBUG 5 freopen("input.txt","r",stdin); 6 #endif 7 8 int q; 9 cin>>q; 10 for(int i=0;i<q;i++){ 11 int n,k; 12 cin>>n>>k; 13 vector<int> a(n); 14 for(int j=0;j<n;j++){ 15 cin>>a[j]; 16 } 17 int mn= *min_element(a.begin(),a.end()); 18 int mx= *max_element(a.begin(),a.end()); 19 if(mx-mn>2*k) cout<<-1<<endl; 20 else cout<<mn+k<<endl; 21 } 22 return 0; 23 }
向量数组(vector<int> a)自带有求取最大值( *max_element(a.begin(),a.end())和最小值(*min_element(a.begin(),a.end()))的函数哦!!!