题目连接:Sherlock and MiniMax

Watson gives Sherlock an array A1,A2...AN
He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ 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 ≤ N ≤ 102
1 ≤ Ai ≤ 109 
1 ≤ P ≤ Q ≤ 109

Sample Input

3
5 8 14
4 9

Sample Output

4

题解:难度为Diffcult的一道题,真的好难=。=

首先题目理解:给定一个数组a和两个整数P和Q,在区间[P,Q]中找到一个数M,使得min{a[i] - M}最大,即对于每一个[P,Q]之间的数m,将数组中的每个数和m求差的绝对值,那么这些绝对值里面就有一个最小值(比如题目中的例子,选择m=4,那么对应的3个差的绝对值是1,4,10,最小值就是1)。现在要做的就是选出这个m,使得这个最小值最大(比如如果选定m=5,那么对应的3个差的绝对值是0,3,9,最小值是0,就比刚才选4的时候得到的最小值小,所以m选4不选5)。

首先对原数组排序,那么对于[P,Q]之间的任意一个m,对应的差的绝对值如下图两种情况所示:

图一

在第一种情况中,最小值在端点处达到;在第二种情况中,最小值在转折点处达到,这都是由a数组有序以后得到的性质。那么我们想想在哪些地方可以达到最大的最小值。如小图所示,对于任意两个数,当且仅当取m为它们的中点(a[i]+a[i+1])/2的时候,得到的最小绝对值最大。所以就可以遍历所有的(a[i],a[i+1]),然后找出使得最小值最大的m作为M。

图二

在这个过程中,有一种情况就是(a[i],a[i+1])的中点不在[P,Q]内,那么最小值或者在[P,Q]中最靠近中点处取得(区间[P,Q]和区间[a[i],a[i+1]]有交集时候,在P或者Q取到最小值);或者[P,Q]和[a[i],a[i+1]]没有交集,即图一中第一种情况,最小值也在P或者Q处达到。那么我们为单独用O(n)的时间考察P和Q,遍历数组,找到最小值,然后查看是否可能成为最大的最小值。

总结一下算法:

  • 首先对数组a排序
  • 对于P和Q,利用O(N)的时间遍历数组,找到最小值,看能够成为最大的最小值;
  • 对于任意(a[i],a[i+1]),考察(a[i]+a[i+1])/2是否在[P,Q]内,如果在,考察此处得到的最小值是否能够成为最大的最小值,注意a[i]+a[i+1]为奇数时,要考察两个中点,它们都可能取得最小值。
  • 算法排序时间复杂度O(NlogN),单独处理P,Q及中点复杂度O(N),所以最终的算法时间复杂度为O(NlogN)。

代码如下:

 import java.io.*;
import java.util.*;
import java.math.*; public class Solution {
static int miniMax = 0;
static int miniMax_index = 0;
private static int mini_first_last(int[] a,int first_last){
//check for p
int mini = Integer.MAX_VALUE; for(int i = 0;i < a.length;i++){
mini = Math.min(mini,Math.abs(a[i]-first_last));
} return mini;
}
private static void middle(int index,int[] a,int p,int q){
int mini = Integer.MAX_VALUE;
int mini_index = 0;
int mid = (a[index]+a[index+1])/2;
if(mid >= p && mid <= q){
int m = Math.abs(a[index]-mid);
int n = Math.abs(a[index+1]-mid);
if(mini > Math.min(m, n)){
mini = Math.min(m, n);
mini_index = mid;
}
if(mid+1<=q && mid*2 != a[index] + a[index+1]){
m = Math.abs(a[index]-mid-1);
n = Math.abs(a[index+1]-mid-1);
if(mini > Math.min(m, n)){
mini = Math.min(m, n);
mini_index = mid+1;
}
}
if(mini != Integer.MAX_VALUE && miniMax < mini){
miniMax = mini;
miniMax_index = mini_index;
}
} }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;i++)
a[i] = in.nextInt();
int p = in.nextInt();
int q = in.nextInt();
Arrays.sort(a); miniMax = mini_first_last(a, p);
miniMax_index = p; int temp = mini_first_last(a, q);
if(miniMax < temp){
miniMax = temp;
miniMax_index = q;
} for(int i = 0;i < n-1;i++){
middle(i, a, p, q);
}
System.out.println(miniMax_index);
}
}

【HackerRank】Sherlock and MiniMax的更多相关文章

  1. 【HackerRank】Sherlock and Array

    Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in ...

  2. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  3. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  4. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  5. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  6. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

  7. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  8. 【HackerRank】Coin on the Table

    题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...

  9. 【HackerRank】Pairs

    题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...

随机推荐

  1. 浅谈web前端安全

    单纯地在你的客户端弹出信息只是类似于迫使你在自己的房间脱衣服--没人看得到,自然也不算啥恶意行为.那么如果我把你的信息通过脚本发送到我的服务器保存起来呢?先放心,我不打算这么做,也没那笔闲钱去购置一个 ...

  2. vSphere ESXi主机配置iSCSI存储

    vSphere ESXi主机配置iSCSI存储 vSphere ESXi主机一般连接的存储类型有光纤存储.iSCSI存储两类.本次案例为iSCSI存储连接ESXi主机的配置. 案例环境:ESXi主机通 ...

  3. 第一百六十九节,jQuery,基础事件

    jQuery,基础事件 学习要点: 1.绑定事件 2.简写事件 3.复合事件 JavaScript 有一个非常重要的功能,就是事件驱动.当页面完全加载后,用户通过鼠标 或键盘触发页面中绑定事件的元素即 ...

  4. html表格上下居中

    在表格外面嵌套另一个高度为 100% 的表格,外层的表格设置 td 的 valign 属性 为 middle <table width="100%" height=" ...

  5. linux tableau server 连接 presto

    记录一下这个弄个好久的难题 linux tableau server 版本  tableau-server-2018-2-0.x86_64.rpm 安装过程 我参照了这儿仁兄   http://ju. ...

  6. 理解和使用WPF 验证机制(值得推荐)

    首先建立一个demo用以学习和实验WPF Data Validation机制.创建一个数据实体类: public class Employee { public string Name { get;  ...

  7. python3 - property的使用

    传统的绑定属性值,会把属性暴露出去,而且无法检查参数是否合法,如下: class Test(object): def  __int__(self,age): self.age = age 为了检查参数 ...

  8. Core Services层

    本文转载至 http://jingyan.baidu.com/article/cdddd41c57360853cb00e124.html Core Services层是系统很多部分的基础部分,也许应用 ...

  9. caffe小问题汇总(持续更新)

    PS:所有问题均在caffe-windows下产生 1.为什么AlexNet中,InnerProduct_Layer(fc8)层的输出可以直接作为Accuracy_Layer层的输出? 答:首先,我们 ...

  10. Android UI开发第二十九篇——Android中五种常用的menu(菜单)

    Android Menu在手机的应用中起着导航的作用,作者总结了5种常用的Menu. 1.左右推出的Menu 前段时间比较流行,我最早是在海豚浏览器中看到的,当时耳目一新.最早使用左右推出菜单的,听说 ...