1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| int helper(vector<int>& A, int k) { auto lb = lower_bound(begin(A), end(A), k); if(lb == end(A)) { return k - A.back(); } else if(lb == begin(A)) { return A[0] - k; } else { return min(abs(*lb - k), abs(*(prev(lb)) - k)); } }
int sherlockAndMinimax(vector<int> A, int p, int q) { sort(begin(A), end(A)); int pmi = helper(A, p), qmi = helper(A, q); int mi = max(qmi, pmi); int res = qmi > pmi ? q : p; for(int i = 0; i < A.size() - 1; i++) { int d = (A[i + 1] - A[i]) / 2; int target = A[i] + d; if(p <= target and target <= q) { if(d == mi) { res = min(res, target); } else if(d > mi) { mi = d; res = target; } } } return res; }
|