HackerRank - "Sherlock and MiniMax"

Note this smart observation from Editorial: "M will be either P or Q or (A_i+A_j)/2".. 

Yea I know, my code is not condense enough..

#include <cmath>
#include <cstdio>
#include <cmath>
#include <climits>
#include <cctype>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int inRange(int p, int q, int v)
{
    return v >= p && v <= q;
}

int main()
{
    int n; cin >> n;
    vector<int> in(n);
    for (int i = 0; i < n; i++)
        cin >> in[i];

    std::sort(in.begin(), in.end());
    int p, q; cin >> p >> q;

    int dist = INT_MIN;
    int v = -1;

    if (q < in[0])
    {
        v = q;
    }
    else if (p > in.back())
    {
        v = p;
    }
    else
    {
        auto it_p = std::lower_bound(in.begin(), in.end(), p);

        //    Check P's
        if (it_p != in.end())
        {
            int dist_p1 = *it_p - p; // p-->in[i]
            int dist_p0 = INT_MAX;     // in[i]-->p
            if (it_p != in.begin())
            {                
                auto it_pp = it_p - 1;
                dist_p0 = p - *(it_pp);

                int mid = (*it_pp + *it_p) / 2;
                if (mid > p)    it_p = it_pp;
            }
            dist = std::min(dist_p0, dist_p1);
            v = p;
        }
        
        auto it_q = std::upper_bound(in.begin(), in.end(), q);        
        
        //    Check mid-points
        for (auto it = it_p; it != it_q; it++)
        {
            int mid = (*it + *(it + 1)) / 2;
            int newdist = (*(it + 1) - *it) / 2;
            if (inRange(p, q, mid) && (newdist > dist))
            {
                dist = newdist;
                v = mid;
            }
        }

        //    Q's
        int newdist_q = INT_MIN;
        if (it_q == in.end())
        {
            newdist_q = q - in.back();
        }
        else if (it_q != in.begin())
        {
            newdist_q = std::min(q - *(it_q - 1), *it_q - q);
        }
        if (newdist_q > dist)
        {
            dist = newdist_q;
            v = q;
        }
    }

    cout << v << endl;
    return 0;
}

转载于:https://www.cnblogs.com/tonix/p/4543146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值