Mind Control (Codeforces 1291C)
Tags: Greedy, data structures

You and your $$$n - 1$$$ friends have found an array of integers $$$a_1, a_2, \dots, a_n$$$. You have decided to share it in the following way: All $$$n$$$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the $$$m$$$-th position in the line. Before the process starts, you may choose up to $$$k$$$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer $$$x$$$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $$$x$$$?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le m \le n \le 3500$$$, $$$0 \le k \le n - 1$$$)  — the number of elements in the array, your position in line and the number of people whose choices you can fix.The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$)  — elements of the array.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3500$$$.

Output: For each test case, print the largest integer $$$x$$$ such that you can guarantee to obtain at least $$$x$$$.

Note: In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.  the first person will take the last element ($$$5$$$) because he or she was forced by you to take the last element. After this turn the remaining array will be $$$[2, 9, 2, 3, 8]$$$;  the second person will take the first element ($$$2$$$) because he or she was forced by you to take the first element. After this turn the remaining array will be $$$[9, 2, 3, 8]$$$;  if the third person will choose to take the first element ($$$9$$$), at your turn the remaining array will be $$$[2, 3, 8]$$$ and you will take $$$8$$$ (the last element);  if the third person will choose to take the last element ($$$8$$$), at your turn the remaining array will be $$$[9, 2, 3]$$$ and you will take $$$9$$$ (the first element). Thus, this strategy guarantees to end up with at least $$$8$$$. We can prove that there is no strategy that guarantees to end up with at least $$$9$$$. Hence, the answer is $$$8$$$.In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $$$4$$$.

Sample Inputs:
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2

Sample Outputs:
8
4
1
1

Solution (Python):
import sys
input = sys.stdin.readline
t = int(input())

class SegTree:

    def segfunc(self, x, y):
        return min(x, y)

    def __init__(self, n, ide_ele, init_val):
        self.ide_ele = ide_ele
        self.num = 2 ** (n - 1).bit_length()
        self.seg = [self.ide_ele] * 2 * self.num
        for i in range(n):
            self.seg[i + self.num - 1] = init_val[i]
        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k + 1:
            k = (k - 1) // 2
            self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])

    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q - p > 1:
            if p & 1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q & 1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        if p == q:
            res = self.segfunc(res, self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
        return res
for _ in range(t):
    (N, M, K) = map(int, input().split())
    a = list(map(int, input().split()))
    dp = [0] * M
    for l in range(M):
        r = M - l - 1
        dp[l] = max(a[l], a[-1 - r])
    res = 0
    if K >= M - 1:
        print(max(dp))
        continue
    if K == 0:
        print(min(dp))
        continue
    seg = SegTree(M, 10 ** 10, dp)
    for l in range(K):
        tt = seg.query(l, min(M, l + M - K))
        res = max(res, tt)
    dp.reverse()
    seg = SegTree(M, 10 ** 10, dp)
    for l in range(K):
        tt = seg.query(l, min(M, l + M - K))
        res = max(res, tt)
    print(res)

Solution (Python):
t = int(input())
for _ in range(t):
    (n, m, k) = map(int, input().split())
    k = min(m - 1, k)
    a = list(map(int, input().split()))
    a2 = [a[i] if a[i] > a[j] else a[j] for (i, j) in zip(range(n), range(n - m, n))]
    (front, back) = ([(a2[0], a2[0])], [])
    for i in range(1, m - k):
        front.append((a2[i], min(a2[i], front[-1][1])))
    ans = front[-1][1]
    inf = 10 ** 9
    for i in range(m - k, m):
        front.append((a2[i], min(front[-1][1], a2[i]) if front else a2[i]))
        if not back:
            while front:
                x = front.pop()[0]
                back.append((x, min(x, back[-1][1]) if back else x))
        back.pop()
        ans = max(ans, min(front[-1][1] if front else inf, back[-1][1] if back else inf))
    print(ans)

Solution (Python):
from collections import deque
t = int(input())
for _ in range(t):
    (n, m, k) = map(int, input().split())
    k = min(m - 1, k)
    a = list(map(int, input().split()))
    a2 = [a[i] if a[i] > a[j] else a[j] for (i, j) in zip(range(n), range(n - m, n))]
    (front, back) = (deque([(a2[0], a2[0])]), deque())
    for i in range(1, m - k):
        front.appendleft((a2[i], min(a2[i], front[0][1])))
    ans = front[0][1]
    inf = 10 ** 9
    for i in range(m - k, m):
        front.appendleft((a2[i], min(front[0][1], a2[i]) if front else a2[i]))
        if not back:
            while front:
                x = front.popleft()[0]
                back.appendleft((x, min(x, back[0][1]) if back else x))
        back.popleft()
        ans = max(ans, min(front[0][1] if front else inf, back[0][1] if back else inf))
    print(ans)