HackerRank and MiniMax
Watson gives Sherlock an array $A_1,A_2\cdots A_N$.
He asks him to find an integer $M$ between $P$ and $Q$ (both inclusive), such that,$\min \{|A_i-M|, 1\le i\le N\}$ is maximised. If there are multiple solutions, print the smallest one.
Input Format
The first line contains $N$. The next line contains space separated $N$ integers, and denote the array $A$. The third line contains two space separated integers denoting $P$ and $Q$.
Output Format
In one line, print the required answer.
Constraints
$1 \le N \le 10^2$
$1 \le A_i \le 10^9$
$1 \le P \le Q \le 10^9$
Sample Input
3
5 8 14
4 9
Sample Output
4
Explanation
For $M$ = 4,6,7, or 9, the result is 1. Since we have to output the smallest of the multiple solutions, we print 4.
Solution
二分答案+$O(N)$构造
复杂度$O(N\log{Q})$
Implementation
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
int n, p, q;
typedef pair<int,int> P;
vector<P> interv[];
vector<int> a;
int now, pre;
void merge(P &a, P &b){
int x=max(a.X, b.X);
int y=min(a.Y, b.Y);
if(x<=y) interv[pre].pb({x, y});
}
const int INF=INT_MAX;
bool C(int x){
pre=, now=;
interv[pre].clear();
interv[now].clear();
interv[now].pb({p, q});
P l, r;
for(int i=; i<a.size(); i++){
for(int j=; j<interv[now].size(); j++){ l={-INF, a[i]-x};
r={x+a[i], INF};
merge(interv[now][j], l);
merge(interv[now][j], r);
}
if(interv[pre].empty()) return false;
interv[now].clear();
swap(pre, now);
}
return true;
}
int bs(int l, int r){ //max
int mid;
while(r-l>){
mid=(l+r)>>;
if(C(mid)) l=mid;
else r=mid;
}
return l;
}
int main(){
//freopen("in", "r", stdin);
scanf("%d", &n);
for(int i=; i<n; i++){
int b;
scanf("%d", &b);
a.push_back(b);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
scanf("%d%d", &p, &q);
C(bs(, 1e9));
sort(interv[now].begin(), interv[now].end());
printf("%d\n", interv[now][].X);
}
总结
二分答案类问题
提法
求满足条件$C(x)$的最大/最小的$x$,其中$C(x)$是一个关于$x$的bool表达式。
这类问题能用二分答案解决的条件是$C(x)$要有单调性,这具体是指
若求使$C(x)$为真的最大的$x$,那么$C(x)$必须满足
若$C(x_0)$为真,则 $\forall x \le x_0, C(x)$为真
若求使$C(x)$为真的最小的$x$,那么$C(x)$必须满足
若$C(x_0)$为真,则 $\forall x \ge x_0,C(x)$为真
HackerRank and MiniMax的更多相关文章
- 【HackerRank】Sherlock and MiniMax
题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- HackerRank "Lucky Numbers"
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
随机推荐
- linux如何挂载windows下的共享文件
说明:windows下有一共享文件夹APP,windows本地ip是192.168.9.155现在需要在linux服务器上挂载这个APP文件夹,linux服务器ip是192.168.9.200 操作记 ...
- jquery.validate运用和扩展
一.运用 默认校验规则 ().required:true 必输字段 ().remote:"remote-valid.jsp" 使用ajax方法调用remote-valid.jsp验 ...
- tftp从linux下载文件
1,背景: 当我们ssh到一台linux上时候,从linux上下载一些文件,方案如下: 1.1通过sftp:通过win7 ftp客户端连接到linux去下载文件. 1.2通过tftp 2,问题 有些l ...
- 快钱支付与Sql Server的乐观锁和悲观锁
在实际的多用户并发访问的生产环境里边,我们经常要尽可能的保持数据的一致性.而其中最典型的例子就是我们从表里边读取数据,检查验证后对数据进行修改,然后写回到数据库中.在读取和写入的过程中,如果在多用户并 ...
- Qt——鼠标拖动调整窗口大小
要求:鼠标移到界面边角时,鼠标样式相应地发生改变. 实现方法一: 重写mouseMoveEvent,如果鼠标没有按下,则根据鼠标在界面上的位置设置鼠标样式,如果鼠标按下,则根据位置判断该怎样调整界面大 ...
- 解决SaveChanges会Hold住之前的错误的问题
问题描述: 在一次新增操作中,由于有一个必填字段忘记写了,然后直接点击提交,运行到savechanges的地方,程序报错,提示***字段为必填字段. 然后关掉页面,重新填写一次,这次什么都填写上了,一 ...
- iOS:界面适配(三)--iPhone不同机型或设备不同尺寸适配(屏幕适配)和系统适配
对于不同苹果设备,各个参数查看<iOS:机型参数.sdk.xcode各版本>. 机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片 ...
- 分享我收集的引擎、图形学、WebGL方面的电子资料
本文分享我这一年以来收集的我认为比较经典的电子资料,希望能对大家有所帮助! 本文会不断更新! 目录 WebGL Insights OpenGL Insights Game Programming Pa ...
- 简述WebService的使用(二)
上集回顾 上一篇我简单的介绍了一下整个WebService建立和后端访问的过程,如果感兴趣可以看一看:简述WebService的使用(一) //如有不懂请留言,觉得有用请点赞 内容提要 这一篇主要介绍 ...
- UEditor编辑文章出现多余空行问题的解决办法
网站后台管理编辑文章以后,保存.再次打开编辑是文章前后多出多余的空行.一直以为是编辑器有问题,也没在意,但是自己编辑文章多了,感觉太麻烦了.想一定可以解决这个问题.百度一下果然找到原因.原来是加载编辑 ...