Codeforces Round #570 (Div. 3) B. Equalize Prices
原文链接https://codeforces.com/contest/1183/problem/B
题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai-bi|<=k并且bi最大。如果找不到,就输出-1.
思路:这个也就关系到,最大值max,最小值min,和K,至于中间的数,我们不用去管,现在一个排序,如果max-min<=2*k,那么就输出min+k,否者就输出-1。(为什么要在2*k的范围找???想想看)
代码:
#include"iostream"
#include"algorithm"
#include"cstring"
#include"cstdio"
#include"cmath"
using namespace std;
int main(){
int q;
while(cin>>q){
while(q--){
int flag=-2;
long long n,k,a[100];
cin>>n>>k;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
if(a[n-1]-a[0]<=2*k) cout<<a[0]+k<<endl;
else cout<<"-1"<<endl;
}
}
return 0;
}
Codeforces Round #570 (Div. 3) B. Equalize Prices的更多相关文章
- Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)
B题题意: 给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k.找到最大的b输出,如果找不到,那就输出-1 题解: 很简单嘛,找到上下限直接二分.下限就是所有物品中最 ...
- Codeforces Round #590 (Div. 3) A. Equalize Prices Again
链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...
- (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #570 Div. 3
A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...
- Codeforces Round #570 (Div. 3) E. Subsequences (easy version) (搜索,STL)
题意:有一长度为\(n\)的字符串,要求得到\(k\)不同的它的子序列(可以是空串),每个子序列有\(|n|-|t|\)的贡献,求合法情况下的最小贡献. 题解:直接撸个爆搜找出所有子序列然后放到set ...
- Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)
题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...
- Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)
题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- wxpython 简单例子:显示文本框的窗口显示鼠标位置
简单例子来自教程: #!/bin/env python import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__ ...
- 清北学堂—2020.1提高储备营—Day 1 afternoon(二分、分治、贪心)
qbxt Day 1 afternoon --2020.1.17 济南 主讲:李佳实 目录一览 1.二分法 2.分治 3.贪心 总知识点:基础算法 一.二分法 (1)算法分析:二分法是一种暴力枚举的优 ...
- 论文阅读笔记(十九)【ITIP2017】:Super-Resolution Person Re-Identification With Semi-Coupled Low-Rank Discriminant Dictionary Learning
Introduction (1)问题描述: super resolution(SP)问题:Gallery是 high resolution(HR),Probe是 low resolution(LR). ...
- PP: Unsupervised anomaly detection via variational auto-encoder for seasonal KPIs in web applications
Problem: unsupervised anomaly detection for seasonal KPIs in web applications. Donut: an unsupervise ...
- Docker容器Centos不能使用systemctl命令问题
注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/bug-dock ...
- C# WPF Bing地图展示
微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言: 如果您觉得Dotnet9对您有帮助,欢迎赞赏 内容目录 实现效果 业务场景 编码实现 本文参考 源码下载 1.实现效果 Bi ...
- xadmin使用
xadmin使用 官方 使用参考
- python 的eval函数
python中的eval()函数是用来计算所有数学的代数计算式,这样可以很快得到复杂代数式的结果. 例如:383660347*375705824-1796136991-1726898699*18994 ...
- LaTeX技巧001:LaTeX中输入大小写罗马数字
\uppercase\expandafter{\romannumeral20} \romannumeral20 前者将输入大写罗马数字20, 后者将输入小写罗马数字20.
- 转: OSIP协议栈使用入门
转自百度文库 很长时间之前,简单粗略地看了下Osip,eXosip,ortp等并快速“封装”了一个Windows下的基于VC6的MFC的SIP软电话(全部源代码VC6工程文件及Lib库可在本Blog共 ...