传送门

Sherlock and MiniMax

Authored by darkshadows on May 07 2014

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的更多相关文章

  1. 【HackerRank】Sherlock and MiniMax

    题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer  ...

  2. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  3. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

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

  5. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  6. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  7. HackerRank "Lucky Numbers"

    Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...

  8. HackerRank "Playing with numbers"

    This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...

  9. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

随机推荐

  1. 【转】【C#】判断两个文件是否相同

    使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否相同 该哈希算法为一个文件生成一个小的二进制“指纹”,从统计学 ...

  2. C++ 排序函数 sort(),qsort()的用法

    转自:http://blog.csdn.net/zzzmmmkkk/article/details/4266888/ 所以自己总结了一下,首先看sort函数见下表: 函数名 功能描述 sort 对给定 ...

  3. 3D数学基础:3D游戏动画中欧拉角与万向锁的理解

    首先来看一下什么是欧拉角(Euler angles)?构件在三维空间中的有限转动,可依次用三个相对转角表示,即进动角.章动角和自旋角,这三个转角统称为欧拉角.——引自百度百科莱昂哈德·欧拉用欧拉角来描 ...

  4. EBS 用户及其联系人的失效时间

    联系人失效时间还有一个SQL,从页面的联系人详情简化取得的,不如直接用pos_supplier_users_v视图 SELECT * FROM (SELECT NULL contact_req_id, ...

  5. Flume的安装与配置

    Flume的安装与配置 一.       资源下载 资源地址:http://flume.apache.org/download.html 程序地址:http://apache.fayea.com/fl ...

  6. IBatis.Net学习笔记六--再谈查询

    在IBatis.Net学习笔记五--常用的查询方式 中我提到了一些IBatis.Net中的查询,特别是配置文件的写法. 后来通过大家的讨论,特别是Anders Cui 的提醒,又发现了其他的多表查询的 ...

  7. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(1)-- 绘写文字

    [概览] 本文实现如下的程序:(在窗体中绘画出文字) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到eric 6, ...

  8. 在opencv3中利用SVM进行图像目标检测和分类

    采用鼠标事件,手动选择样本点,包括目标样本和背景样本.组成训练数据进行训练 1.主函数 #include "stdafx.h" #include "opencv2/ope ...

  9. 记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)

    项目上线以来一直存在一个比较揪心的问题,和一个没有信心处理的BUG,那就是在应用程序启动时有可能会导致cpu跑满99%或持续在一个值如50%左右,这样一来对服务器的压力是非常大的,经常出现服务器无法远 ...

  10. on 在ios下 父对象是body的时候会 不调用

    on 用委托的形式绑定事件 在ios下 父对象是body 获取 docment的时候会 不调用 解决方案  在这些元素上加 cursor: pointer;