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/ ...
随机推荐
- Android SQLite (二) 基本用法
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的. 一 ...
- 30Springd的包扫描——<context:component-scan base-package=” ”/>
在context中配置 如:在base-package指明一个包: <context:component-scan base-package="cn.edu.dao"/> ...
- linux patch 格式与说明(收录)
转:http://blog.chinaunix.net/uid-26813001-id-3282954.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一下,那 ...
- HTTP基础(一):如何使用浏览器network查看请求和响应的信息
一. 问题描述 HTTP作为前端开发与后开发链接的载体,其重要性不言而喻,今天我不复习关于HTTP自身的一些知识,只复习如何解读浏览器自带的的抓包工具(查看请求信息与响应信息)network. 二. ...
- WPF Binding INotifyPropertyChanged 多线程 深入理解
例子 先来看一个例子 Person.cs public class Person : ObservableObject,INotifyPropertyChanged { private string ...
- 解答WPF中ComboBox SelectedItem Binding不上的Bug
正在做一个打印机列表,从中选择一个打印机(System.Printing) <ComboBox Width="150" ItemsSource="{Binding ...
- eclipse项目编码问题
使得eclipse的新建项目的默认编码直接为UTF-8: 在菜单栏的Window->Preferences->General->Workspace->Text file enc ...
- Object C学习笔记21-typedef用法
在上一章的学习过程中遇到了一个关键字typedef,这个关键字是C语言中的关键字,因为Object C是C的扩展同样也是支持typedef的. 一. 基本作用 typedef是C中的关键字,它的主要作 ...
- 那天有个小孩跟我说LINQ(八)学会Func
文章已经迁移到:http://www.ayjs.net/2013/08/68/ 文章已经迁移到:http://www.ayjs.net/2013/08/68/ 文章已经迁移到:http://www.a ...
- Source Tree for MAC1.6
Atlassian ID has become the new Atlassian Account. Read more about it here. After some great communi ...