Problem:


Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8



Sample input:
3
10 30 20


Sample output:
30

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

import copy
import statistics as st

b = copy.deepcopy(a)
b.sort()
bb = copy.deepcopy(b)
lena = len(a)
lenb = lena

mid_index = lenb//2
mid = b[mid_index]

ans = []
for i in range(lena):
    a1 = a[i:]
    lena1 = lena - i
    if i > 0:
        bb.remove(a[i-1])
        mid_index = lena1//2
    bbb = copy.deepcopy(bb)
    for j in range(lena1, 0, -1):
        a2 = a1[:j]
        lena2 = j
        if j < lena1:
            bbb.remove(a1[j])
            mid_index = mid_index  - lena2%2
        ans.append(bbb[mid_index])

print(st.median_high(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

le = -1
ri = max(A) + 1
mid = (le + ri) // 2
while ri - le > 1:
    S = [0] * n
    S[0] = -1 + 2 * int(A[0] >= mid)
    for i in range(n-1):
        S[i+1] = S[i] - 1 + 2 * int(A[i] >= mid)
    bit = Bit(n+2)
    ans = 0

    Smin = min(S)
    S = [S[i]-min(S)+1 for i in range(n)]
    #print(S)
    for i, p in enumerate(S):
        bit.add(p, 1)
        ans += bit.sum(p) - 1  # 「S[i] >= S[i]」をカウントしないように1を引く

    #print(le, ri, mid, ans)

    if ans >= (n*(n-1))//(2*2):
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2

print(le)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

le = -1
ri = max(A) + 1
mid = (le + ri) // 2
while ri - le > 1:
    Ac = [-1 + 2 * int(A[i] >= mid) for i in range(n)]
    S = [0] * n
    S[0] = Ac[0]
    for i in range(n-1):
        S[i+1] = S[i] + Ac[i+1]
    bit = Bit(n+2)
    ans = 0


    Smin = min(S)
    S = [S[i]-min(S)+1 for i in range(n)]
    #print(S)
    for i, p in enumerate(S):
        bit.add(p, 1)
        ans += bit.sum(p) - 1  # 「S[i] >= S[i]」をカウントしないように1を引く

    #print(le, ri, mid, ans)

    if ans >= n*(n-1)//(2*2):
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2

print(le)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

le = -1
ri = max(A) + 1
mid = (le + ri) // 2
while ri - le > 1:
    Ac = [-1 + 2 * int(A[i] >= mid) for i in range(n)]
    S = [0] * n
    S[0] = Ac[0]
    for i in range(n-1):
        S[i+1] = S[i] + Ac[i+1]
    bit = Bit(n+1)
    ans = 0


    Smin = min(S)
    S = [S[i]-min(S)+2 for i in range(n)]
    #print(S)
    for i, p in enumerate(S):
        bit.add(p, 1)
        ans += bit.sum(p) - 1  # 「S[i] >= S[i]」をカウントしないように1を引く

    #print(le, ri, mid, ans)

    if ans >= n*(n-1)//(2*2):
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2

print(le)

Submission status: Time Limit Exceeded

Solution code:
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
def invnum(n,a):
    bit=Bit(n)
    res=0
    for j in range(n):
        res+=j-bit.sum(a[j])
        bit.add(a[j],1)
    return res
N=int(input())
A=[int(i) for i in input().split()]
def solve(x):
    S=[0 for i in range(N+1)]
    for i in range(N):
        if A[i]<x:
            S[i+1]=S[i]-1
        else:
            S[i+1]=S[i]+1
    T=[seq[1]+1 for seq in sorted([(S[i],i) for i in range(N+1)])]
    return invnum(N+1,T)
low=1
high=max(A)+1
M=(N*(N+1))//2
while(high-low>1):
    mid=(low+high)//2
    if solve(mid)*2<=M:
        low=mid
    else:
        high=mid
print(low)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))

coef = (N * (N + 1) + 2) // 4

A_sorted = sorted(A)

def judge(x):
    S = []
    pre_S = 0
    for a in A:
        if a >= x:
            pre_S += 1
            S.append(pre_S)
        else:
            pre_S -= 1
            S.append(pre_S)

    count = 0
    for l in range(len(S)):
        for r in range(l + 1, len(S)):
            if S[l] <= S[r]:
                count += 1
    
    return count >= coef

right = len(A_sorted) - 1
left = 0
while right - left > 1:
    mid = (right + left) // 2
    if judge(A_sorted[mid]):
        left = mid
    else:
        right = mid

print(A_sorted[right])