传送门

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. ViewPager -- Fragment 切换卡顿 性能优化

    当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...

  2. 考虑与Maya结合

    今天改进了Hessian各块的计算代码,减少了一些内存操作.下一步准备把模拟平台与Maya结合,这样就可以利用Maya丰富的变形算法了. 这一步需要考虑以下问题: 1.把场景设置为某一帧.这一点可以用 ...

  3. App_api设计

    2014年,移动APP的热度丝毫没有减退,并没有像桌面软件被WEB网站那样所取代,不但如此,越来越多的传统应用.网站也都开始制作自己的移动APP,也就是我们常说的IOS客户端.android客户端.这 ...

  4. Entity Framework版本历史概览

    转自:http://www.cnblogs.com/fecktty2013/archive/2014/09/26/entityframework-overview.html EF版本 .net fra ...

  5. scala学习之第三天:数组的特性与使用技巧

    1.数组 Scala数组与Scala序列是兼容的 - 在需要Seq[T]的地方可由Array[T]代替.最后,Scala数组支持所有的序列操作. 隐式转换 方法1:通过scala.collection ...

  6. box-css3

    父容器样式必须有定义:"{ display: -webkit-box }" 现象:水平时只能在一行布局,子容器在垂直方向上会填充父容器. 技巧:可以做水平居中和垂直居中.也可以实现 ...

  7. php基础05:常量

    <?php // 1.PHP 常量介绍 // 常量是单个值的标识符(名称).在脚本中无法改变该值.有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号). // 2设置 PHP 常量 // ...

  8. eclipse使用

    Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台. Eclipse 是 Java 的集成开发环境(IDE),当然 Eclipse 也可以作为其他开发语言的集成开发环境,如C,C++ ...

  9. 在C#代码中应用Log4Net 中配置文件的解释

    一个完整的配置文件的例子如下所示,这个是”在C#代码中应用Log4Net(二)”中使用的配置文件. <log4net> <!-- 错误日志类--> <logger nam ...

  10. C/C++中的结构体

    结构体定义 结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构.   结构体作用 结构体和其他类型基础数据类型一样,例如int类型,char类型 只不过结构体可以做成 ...