• Eqaulize Prices


    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 |aibi|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 |aiB|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.

    Input

    The first line of the input contains one integer qq (1q1001≤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 (1n100,1k1081≤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 (1ai1081≤ai≤108), where aiai is the price of the ii-th product.

    Output

    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 |aiB|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.

    Example
    input
    Copy
    4
    5 1
    1 1 2 3 1
    4 2
    6 4 8 5
    2 2
    1 6
    3 5
    5 2 5
    
    output
    Copy
    2
    6
    -1
    7
    
    Note

    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: |1B|2|1−B|≤2, |6B|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 [aik;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+kmaxkmin+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()))的函数哦!!!

  • 相关阅读:
    对spring web启动时IOC源码研究
    对volatile关键字的理解
    [书籍分享]0-009.微信营销与运营解密:利用微信创造商业价值的奥秘
    [JavaWeb基础] 002.JSP和SERVLET初级入门
    [Objective-C] 005_Category(类别)
    [PHP学习教程
    读Pyqt4教程,带你入门Pyqt4 _008
    读Pyqt4教程,带你入门Pyqt4 _007
    宝宝巴士安卓框架介绍
    [安卓基础] 006.打开另一个Activity
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11198816.html
Copyright © 2020-2023  润新知