博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #570 (Div. 3)B
阅读量:4557 次
发布时间:2019-06-08

本文共 2898 字,大约阅读时间需要 9 分钟。

B - Equalize Prices

题目链接:

题目:

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
#include
#include
#include
#include
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;}

 

转载于:https://www.cnblogs.com/Vampire6/p/11148971.html

你可能感兴趣的文章
Java范例集锦(二)
查看>>
C语言变量和常量
查看>>
LInuxDay8——shell脚本编程基础
查看>>
topcoder 673
查看>>
Java中一些常用的类,包,接口
查看>>
下载特定区域内街景照片数据 | Download Street View Photos within Selected Region
查看>>
StarUML 破解方法
查看>>
C语言结构体
查看>>
[转]Tribon船体生产设计应用
查看>>
easy ui datagrid 让某行复选框不能选中
查看>>
第六周作业
查看>>
关于adb端口被占用的解决办法
查看>>
php 部分内置函数的使用
查看>>
字符串处理技巧
查看>>
归档及压缩命令
查看>>
Mybatis步骤
查看>>
WPF自定义控件之扩展原生控件
查看>>
《区块链100问》笔记整理——42~49问
查看>>
使用Jquery+EasyUI 进行框架项目开发案例讲解之二---用户管理源码分享
查看>>
深入理解计算机系统(1.4)---并发与并行、浅谈抽象
查看>>