zoukankan      html  css  js  c++  java
  • Codeforces Round #570 (Div. 3)B

    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.
     
    题意:给出两个数,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;
    }
  • 相关阅读:
    两款开发辅助工具介绍
    探究Repository模式的两种写法与疑惑
    js 时间处理
    Jquery元素追加和删除
    js 格式验证总结
    jquery UI datepicker时间控件的使用
    jquery 实现 点击按钮后倒计时效果,多用于实现发送手机验证码、邮箱验证码
    JS 字符串编码函数(解决URL特殊字符传递问题):escape()、encodeURI()、encodeURIComponent()区别详解
    form表单和ajax表单提交(Html.BeginForm()、Ajax.BeginForm())的差别
    了解了这些才能开始发挥jQuery的威力(转)
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11148971.html
Copyright © 2011-2022 走看看