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: Wrong Answer

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

get_ints = lambda: [int(st) for st in input().split()]
N=int(input())
inarr=get_ints()

sorted_in = sorted(inarr)
comb = N * (N-1) // 2

def binsearch(lft, rgt):
    half = (lft + rgt) // 2
    # print(lft, rgt, sorted_in[half])
    if lft >= half:
        return lft
    summed = 0
    res = 0

    bit = Bit(2*N+1)
    for i, value in enumerate(map(lambda x: -1 if x < sorted_in[half] else 1, inarr)):
        summed += value
        # print("summed: {}, bit: {}".format(summed, i - bit.sum(N + summed)))
        count = i - bit.sum(N + summed)
        bit.add(N + summed, 1)
        res += count

    # print(res)
    if res <= (comb // 2):
        return binsearch(half, rgt)
    else:
        return binsearch(lft, half)

# print(sorted_in)
res = binsearch(0, N)
# print(res, sorted_in[res])
print(sorted_in[res])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int, input().split()))
s.sort()
s = s[n//2:]
print(s[len(s)//2])

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
lst = 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
 
            
def judge(x, lst, n):
 
    length = len(lst)
    all_pair = (length*(length-1) // 2) + length
    
    lst_r = [-1 if i < x else 1 for i in lst]
    lst_accum = []
    a = 0
    for i in lst_r:
        a += i
        lst_accum.append(a)
    
    bit = Bit(n)
    lst_accum_sorted = {b: i for i, b in enumerate(sorted(lst_accum), start=1)}
    count = 0
    for j, b in enumerate(lst_accum):
        count += bit.sum(lst_accum_sorted[b]) + (b >= 0)
        bit.add(lst_accum_sorted[b], 1)
        
    if count >= math.ceil(all_pair/2):
        return True
    else:
        return False
 
    
sorted_lst = sorted(lst)
low = 0
high = len(lst) - 1
while high - low >1:
    t = (low+high) // 2
    i = sorted_lst[t]
    if judge(i, lst, n):
        low = t
    else:
        high = t
        
print(sorted_lst[low])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = 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
 
            
def judge(x, lst):
 
    length = len(lst)
    all_pair = (length*(length-1) // 2) + length
    
    lst_r = [-1 if i < x else 1 for i in lst]
    lst_accum = []
    a = 0
    for i in lst_r:
        a += i
        lst_accum.append(a)
    min_a = min(lst_accum)
    lst_accum = [i - min_a + 1 for i in lst_accum]
    
    bit = Bit(max(lst_accum))
    ans = 0
    for i, p in enumerate(lst_accum):
        bit.add(p, 1)
        ans += i + 1 - bit.sum(p)
    
    ans = all_pair - ans
    if ans >= (all_pair//2) + 1:
        return True
    else:
        return False
 
    
sorted_lst = sorted(lst)
low = 0
high = len(lst) - 1
t = (low+high) // 2
while high - low >1:
    i = sorted_lst[t]
    if judge(i, lst):
        if low == t:
            break
        low = t
        t = (low+high) // 2
    else:
        if high == t:
            break
        high = t
        t = (low+high) // 2
        
print(sorted_lst[high]) if judge(sorted_lst[high], lst) else print(sorted_lst[low])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = 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
 
            
def judge(x, lst):
 
    length = len(lst)
    all_pair = (length*(length-1) // 2) + length
    
    lst_r = [-1 if i < x else 1 for i in lst]
    lst_accum = []
    a = 0
    for i in lst_r:
        a += i
        lst_accum.append(a)
    min_a = min(lst_accum)
    lst_accum = [i - min_a + 1 for i in lst_accum]
    
    bit = Bit(max(lst_accum))
    ans = 0
    for i, p in enumerate(lst_accum):
        bit.add(p, 1)
        ans += i + 1 - bit.sum(p)
    
    ans = all_pair - ans
    if ans >= (all_pair//2) + 1:
        return True
    else:
        return False
 
    
sorted_lst = sorted(lst)
low = 0
high = len(lst) - 1
t = (low+high) // 2
while low < high:
    i = sorted_lst[t]
    if judge(i, lst):
        if low == t:
            break
        low = t
        t = (low+high) // 2
    else:
        if high == t:
            break
        high = t
        t = (low+high) // 2
        
print(sorted_lst[high]) if judge(sorted_lst[high], lst) else print(sorted_lst[low])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = 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

            
def judge(x, lst):

    length = len(lst)
    all_pair = (length*(length-1) // 2) + length
    
    lst_r = [-1 if i < x else 1 for i in lst]
    lst_accum = []
    a = 0
    for i in lst_r:
        a += i
        lst_accum.append(a)
    min_a = min(lst_accum)
    lst_accum = [i - min_a + 1 for i in lst_accum]
    
    bit = Bit(max(lst_accum))
    ans = 0
    for i, p in enumerate(lst_accum):
        bit.add(p, 1)
        ans += i + 1 - bit.sum(p)
    
    ans = all_pair - ans
    if ans >= (all_pair//2) + 1:
        return True
    else:
        return False

    
lst = sorted(lst)
low = 0
high = len(lst) - 1
t = (low+high) // 2
while low < high:
    i = lst[t]
    if judge(i, lst):
        if low == t:
            break
        low = t
        t = (low+high) // 2
    else:
        if high == t:
            break
        high = t
        t = (low+high) // 2
        
print(lst[high]) if judge(lst[high], lst) else print(lst[low])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = 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

            
def judge(x, lst):

    length = len(lst)
    all_pair = (length*(length-1) // 2) + length
    
    lst_r = [-1 if i < x else 1 for i in lst]
    lst_accum = []
    a = 0
    for i in lst_r:
        a += i
        lst_accum.append(a)
    min_a = min(lst_accum)
    lst_accum = [i - min_a + 1 for i in lst_accum]
    
    bit = Bit(max(lst_accum))
    ans = 0
    for i, p in enumerate(lst_accum):
        bit.add(p, 1)
        ans += i + 1 - bit.sum(p)
    
    ans = all_pair - ans
    if ans >= (all_pair//2) + 1:
        return True
    else:
        return False

    
lst = sorted(lst)
low = 0
high = len(lst) - 1
t = (low+high) // 2
while low < high:
    i = lst[t]
    if judge(i, lst):
        if low == t:
            break
        low = t
        t = (low+high) // 2
    else:
        if high == t:
            break
        high = t
        t = (low+high) // 2
        
print(lst[high]) if judge(lst[high], lst) else print(lst[t])

Submission status: Wrong Answer

Solution code:
import math
 
n = int(input())
a = [int(i) for i in input().split(" ")]
 
m = n * (n + 1) // 2
 
class BIT:
    def __init__(self, n):
        self.t = [0] * (n + 1)
    
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.t[i]
            i -= (i & -i)
        return s
    
    def add(self, i, v):
        while i < len(self.t):
            self.t[i] += v
            i += (i & -i)
 
def count(s):
    s_min = min(s)
    b = BIT(max(s) - s_min + 1)
    t = [0] * len(s)
    for j in range(0, len(s)):
        t[j] = b.sum(s[j] - s_min + 1)
        b.add(s[j] - s_min + 1, 1)
    return sum(t)
            
def bs(l, r):
    if l == r:
        return l
    else:
        h = math.ceil((l + r) / 2)
        
        ab = [-1 if a[i] < h else 1 for i in range(0, n)]
        s = [0] * n
        s[0] = ab[0]
        for i in range(1, n):
            s[i] = s[i - 1] + ab[i]
            
        if count(s) + sum([(i + 1) // 2 for i in ab]) >= math.ceil(m / 2):
            return bs(h, r)
        else:
            return bs(l, h - 1)
        
print(bs(min(a), max(a)))

Submission status: Wrong Answer

Solution code:
class BIT:
    """1-indexed BIT
    """

    def __init__(self, size: int):
        """サイズを指定して BIT を初期化します。
        """
        self.__size = size
        self.__bit = [0] * (self.__size+1)

    def add(self, idx: int, val: int):
        """位置 idx (1-indexed) に値 val をたす。
        :param idx: 値を追加したいインデックス
        :param val: 追加したい値
        """
        while idx <= self.__size:
            self.__bit[idx] += val
            idx += idx & (-idx)

    def sum(self, idx: int)->int:
        s = 0
        while idx > 0:
            s += self.__bit[idx]
            idx -= idx & (-idx)
        return s


def median_of_medians(N: int, A: list) -> int:
    def check(x: int)->int:
        bit = BIT(2 * N + 1)
        s = 0
        a = 0
        for i in range(N):
            if x <= A[i]:
                a += 1
            else:
                a -= 1
            # a = a + (1 if x <= A[i] else - 1)
            s += bit.sum(a + N)
            bit.add(a + N, 1)

        # s += bit.sum(a + N)

        return s

    # alpha = sorted(A)
    l, r = 0, max(A)
    while r - l > 1:
        m = (r + l) // 2 + 1
        # print(l, m, r)
        if check(m) >= (N * (N - 1) // 2) // 2:
            l = m
        else:
            r = m-1

    # return alpha[l]
    return l


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = median_of_medians(N, A)
    print(ans)

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
def med(x):
    cum = [0] * (n+1)
    for i in range(n):
        if a[i] >= x:
            cum[i+1] = cum[i] + 1
        else:
            cum[i+1] = cum[i] - 1
    m = min(cum)
    for i in range(n+1):
        cum[i] -= m - 1
    zb = Bit(max(cum))
    ret = 0
    for j in range(n+1):
        ret += zb.sum(cum[j])
        zb.add(cum[j], 1)
    return ret
b = sorted(a)
l = 0
r = n
while(r - l > 1):
    mid = (l + r + 1) // 2
    print(l,mid,r)
    if 2 * med(b[mid]) >= n * (n+1) // 2:
        l = mid
    else:
        r = mid
print(b[l])

Submission status: Wrong Answer

Solution code:
import copy

def solve():
    N = int(input())
    a = list(map(int, input().split()))
    s = {}
    # *
    # ***
    # *****
    # ****
    # **

    # *
    # ***
    # ****
    # **
    for i in range((N + 1) // 2):
        c = 1 + i * 2
        ai = a[i]
        if ai in s.keys():
            s[ai] += c
        else:
            s[ai] = c
    for i in range((N + 1) // 2, N):
        c = (N - i) * 2
        ai = a[i]
        if ai in s.keys():
            s[ai] += c
        else:
            s[ai] = c

    target = (N * (N + 1) // 2) // 2 + 1
    skeys = sorted(s.keys())

    #print(target)
    #print(s)
    for k in skeys:
        if target > s[k]:
            target -= s[k]
        else:
            return k

ans = solve()
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
median_len=N*(N+1)//2
median=median_len//2+1
a=1
medin_rank=1
for i in range(1,N):
    a=i*(i+1)//2
    b=(i+2)*(i+1)//2
    if a<=median and b>median:
        medin_rank=i
        break
rank_index=median-a
median_list=[]
for i in range(N-medin_rank+1):
    buf=A[i:i+medin_rank]
    buf.sort()
    median_list.append(buf[medin_rank//2])
median_list.sort()
print(median_list[rank_index])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * n

    def sum(self, i):
        s = 0
        i -= 1
        while i >= 0:
            s += self.tree[i]
            i = (i & (i + 1)) - 1
        return s

    def add(self, i, x):
        while i < self.size:
            self.tree[i] += x
            i |= i + 1

def check(a, v):
    bit = Bit(201010)
    offset = 100000

    tot = 0
    t = 0
    bit.add(offset, 1)
    for i in range(len(a)):
        t += 1 if v <= a[i] else -1

        tot += bit.sum(t + offset + 1)
        bit.add(t + offset, 1)

    return int((int(len(a) * (len(a) - 1) / 2) + len(a) + 1) / 2) <= tot

def main():
    N = int(input())
    a = list(map(int, input().split()))

    ok, ng = 1, 10 ** 9

    while ok + 1 != ng:
        md = int((ok + ng) / 2)
        if check(a, md):
            ok = md
        else:
            ng = md


    print(ok)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        i -= 1
        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 check(a, v):
    bit = Bit(201010)
    offset = 100000

    tot = 0
    t = 0
    bit.add(offset, 1)
    for i in range(len(a)):
        t += 1 if v <= a[i] else -1

        tot += bit.sum(t + offset + 1)
        bit.add(t + offset, 1)

    return int((int(len(a) * (len(a) - 1) / 2) + len(a) + 1) / 2) <= tot

def main():
    N = int(input())
    a = list(map(int, input().split()))

    ok, ng = 1, 10 ** 9

    while ok + 1 != ng:
        md = int((ok + ng) / 2)
        if check(a, md):
            ok = md
        else:
            ng = md


    print(ok)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = (int)(input())
l = (list)(map(int, input().split()))
sorted(l)
print(l[n//2])

Submission status: Wrong Answer

Solution code:

from bisect import bisect_left
from math import ceil
import sys
if sys.version_info[0:2] >= (3, 3):
    from collections.abc import Sequence
else:
    from collections import Sequence


class LazySequence(Sequence):
    def __init__(self, f, n):
        self.f = f
        self.n = n

    def __len__(self):
        return self.n

    def __getitem__(self, i):
        if not (0 <= i < self.n):
            raise IndexError
        return self.f(i)


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def __len__(self):
        return self.size

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, v):
        while i <= self.size:
            self.tree[i] += v
            i += i & -i


N = int(input())
a = [0] + [int(s) for s in input().split()]
# a = [0] * (N + 1)


def count_lr(x):
    s = [0] * (N + 1)
    for i in range(1, N + 1):
        s[i] = s[i - 1] + (1 if a[i] >= x else -1)

    ss = sorted(set(s))
    ind = {v: i + 1 for i, v in enumerate(ss)}

    b = Bit(N + 10)
    count = 0
    for i in range(N + 1):
        j = ind[s[i]]
        b.add(j, 1)
        count += b.sum(j)

    return count


# i: 0, 1, ..., 10 ** 9 + 10
# x: 10 ** 9 + 10, ..., 0
counts = LazySequence(lambda i: count_lr(10 ** 9 + 10 - i), 10 ** 9 + 10)
ans = 10 ** 9 + 10 - bisect_left(counts, ceil(N * (N + 1) / 4))
print(ans)

Submission status: Wrong Answer

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

#Sの転倒数
def search(x):
    b = n
    r = 0
    y = 0
    D = [0]*(2*n+1)
    for i in range(n):
        D[b] += 1
        if A[i] < x:
            r += D[b]
            b += 1
        else:
            b -= 1
            r -= D[b]   
        y += r
        print(D)
        print(b, r, y)
    return y


print(search(0))

Submission status: Wrong Answer

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

b = list(set(b))
b.sort()
 
if len(b) > 1:
    l = len(b)//2
    if  l >= len(b) :
        print(b[l-1])
    else:
        print(b[l])
else:
    print(b[0])

Submission status: Wrong Answer

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

#b = list(set(a))
b.sort()
print(b)
 
if len(b) > 1:
    l = len(b)//2
    print(b[l])
else:
    print(b[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
 
if len(a) >= 1:
    print(a[len(a)//2])
else:
    print(a[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
 
b = list(set(a))
b.sort()
 
if len(b) > 1:
    l = len(b)//2
    if l + 1 >= len(b):
        print(b[l])
    else:
        print(b[l+1])
else:
    print(b[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
 
b = list(set(a))
b.sort()
 
if len(b) > 1:
    if len(b) % 2 == 0:
        print(b[len(b)//2])
    else:
        print(b[len(b)//2+1])
else:
    print(b[0])

Submission status: Wrong Answer

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

b.sort()

if len(b) > 1:
    print(len(b))
    if len(b) % 2 == 0:
        print(b[len(b)//2])
    else:
        print(b[len(b)//2+1])
else:
    print(b[0])

Submission status: Wrong Answer

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

b = list(set(a))
b.sort()

if len(b) > 1:
    print(len(b))
    if len(b) % 2 == 0:
        print(b[len(b)//2])
    else:
        print(b[len(b)//2+1])
else:
    print(b[0])

Submission status: Wrong Answer

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

b = list(set(a))
b.sort()

if len(b) > 1:
    print(b[len(b)//2+1])
else:
    print(b[0])

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
num_list = [int(item) for item in input().split()]
search_list = sorted(list(set(num_list)))

def cusum(idx):
  tmp = []
  n = 0
  for item in num_list:
    n = n-1 if item < idx else n+1
    tmp.append(n)
  return tmp


def fall_number(key):
  arr = cusum(key)
  n_max, n_min = max(arr), min(arr)
  n = n_max - n_min + 1
  bit = [0] * n
  ans = 0
  for j, val in enumerate(arr):
    idx = val - n_min + 1
    _sum = 0
    while(idx > 0):
      _sum += bit[idx-1]
      idx -= (idx & -idx)
    idx = val - n_min + 1
    while(idx <= n):
      bit[idx-1] += val
      idx += (idx & -idx)
    ans += j - _sum
  return ans

def binary_serach(th):
  right = len(search_list)
  left = -1
  while(right - left > 1):
    mid = (right + left) // 2
    if fall_number(search_list[mid]) >= th:
      right = mid
    else:
      left = mid
  return left

th = N*(N+1) / 4 
res = binary_serach(th)

print(search_list[res])

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
num_list = [int(item) for item in input().split()]
search_list = list(set(num_list))

def cusum(idx):
  tmp = []
  n = 0
  for item in num_list:
    n = n-1 if item < idx else n+1
    tmp.append(n)
  return tmp

class binary_index_tree():
  def __init__(self, n):
    self.bit = [0] * n
    self.n = n

  def add(self, idx, val):
    while(idx <= self.n):
      self.bit[idx-1] += val
      idx = idx + (idx & -idx)

  def calc(self, idx):
    res = 0
    while(idx > 0):
      res += self.bit[idx-1]
      idx = idx - (idx & -idx)
    return res

def fall_number(idx):
  arr = cusum(idx)
  n_max, n_min = max(arr), min(arr)
  n = n_max - n_min + 1
  BIT = binary_index_tree(n)
  ans = 0
  for val in arr:
    ans += BIT.calc(n) - BIT.calc(val-n_min+1)
    BIT.add(val-n_min+1, 1)
  return ans

def binary_serach(th):
  right = len(search_list)
  left = -1
  while(right - left > 1):
    mid = (right + left) // 2
    if fall_number(search_list[mid]) >= th:
      right = mid
    else:
      left = mid
  return left

th = N*(N+1) / 4 
res = binary_serach(th)
print(search_list[res])

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
num = [int(item) for item in input().split()]
s_list = list(set(num))

def cusum(idx):
  tmp = []
  n = 0
  for item in num:
    n = n-1 if item < idx else n+1
    tmp.append(n)
  return tmp

class binary_index_tree():
  def __init__(self, n):
    self.bit = [0] * n
    self.n = n

  def add(self, idx, val):
    while(idx <= self.n):
      self.bit[idx-1] += val
      idx = idx + (idx & -idx)

  def calc(self, idx):
    res = 0
    while(idx > 0):
      res += self.bit[idx-1]
      idx = idx - (idx & -idx)
    return res

def fall_number(arr):
  n_max, n_min = max(arr), min(arr)
  n = n_max - n_min + 1
  BIT = binary_index_tree(n)
  ans = 0
  for val in arr:
    ans += BIT.calc(n) - BIT.calc(val-n_min+1)
    BIT.add(val-n_min+1, 1)
  return ans 

l = len(s_list)
s = 0
th = (N+N*N) / 4

while(l-s>1):
  mid = (s+l) // 2
  arr = cusum(s_list[mid-1])
  nega = len([item for item in arr if item < 0])
  val = fall_number(arr) + nega
  if val >= th:
    l = mid
  else:
    s = mid
print(s_list[s-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
import numpy as np
ar=np.array(list(map(int,input().split())),dtype=np.int32)
ans=list(ar[:])
for i in range(2,n+1):
    for e in range(0,n+1-i):
        ans.append(np.sort(ar[e:e+i])[i>>1])
print(int(sorted(ans)[len(ans)>>1]))
print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())
import numpy as np
ar=np.array(list(map(int,input().split())),dtype=np.int32)
ans=ar[:]
for i in range(2,n+1):
    for e in range(0,n+1-i):
        np.append(ans,np.median(ar[e:e+i]))
print(int(np.median(np.array(ans))))

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=list(map(int,input().split()))
ans=[]
for i in range(1,n+1):
    for e in range(0,n+1-i):
        ans.append(sorted(lst[e:e+i])[i//2])
print(ans)
print(sorted(ans)[len(ans)//2])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import math
import numpy as np

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


class SegmentTree(object):
    """
    区分和をSegmentTreeで求める

    Prameters
    ---------
    a_list : list
        対象の配列

    Attributes
    ----------
    n_tree : int
        len(a_list) を超える最小の2べきの数

    tree : list, shape = [2*n_tree - 1]
        セグメント木

    参考URL:
    https://www.slideshare.net/iwiwi/ss-3578491
    http://tsutaj.hatenablog.com/entry/2017/03/29/204841
    """
    def __init__(self, a_list):
        self.n_tree = 2 ** math.ceil(math.log2(len(a_list)))
        self.tree = self.initialize_tree(self.n_tree)

    def initialize_tree(self, n):
        """ tree initialization """
        tree = [0 for _ in range(2*n - 1)]
        return tree

    def update(self, i):
        """ update a_i += 1 """
        idx = self.n_tree + i - 1
        self.tree[idx] += 1
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.tree[idx*2 + 1] + self.tree[idx*2 + 2]

    def query(self, a, b, k, left, right):
        """
        Get summation value of range [a, b)
        Usage : query(a, b, 0, 0, n_tree)
        """
        if (right <= a) or (left >= b):
            return 0
        elif (left >= a) and (right <= b):
            return self.tree[k]
        else:
            val_left = self.query(a, b, k*2 + 1, left, (left + right) // 2)
            val_right = self.query(a, b, k*2 + 2, (left + right) // 2, right)
            return val_left + val_right


def calc_inversion_num(x, a_list):
    n_a = len(a_list)
    transformed = np.zeros(n_a)
    transformed[a_list < x] = -1
    transformed[transformed != -1] = 1

    cum_t = transformed.cumsum()
    filtered_t = list(sorted(set(cum_t)))
    st = SegmentTree([0] * len(filtered_t))

    inv = (transformed == -1).sum()
    for i, c in enumerate(cum_t):
        temp_inv = st.query(0, filtered_t.index(c)+1, 0, 0, st.n_tree)
        st.update(filtered_t.index(c))
        inv += i - temp_inv

    return inv


N = d_in()
a_list = ds_in()
a_list = np.array(a_list)
sorted_a = np.sort(a_list)

search_range = [0, N]  # range [0, N)
while search_range[1] - search_range[0] > 1:
    mid = (search_range[0] + search_range[1]) // 2
    inv = calc_inversion_num(sorted_a[mid], a_list)
    if (N * (N+1) / 2 - inv) < math.ceil(N * (N+1) / 4):
        search_range[1] = mid
    else:
        search_range[0] = mid
    print(search_range)

print(sorted_a[search_range[0]])

Submission status: Wrong Answer

Solution code:
A=[1,4,4,5]
A.index(4)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from math import ceil
N = int(input())
A = list(map(int, input().split()))


class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= (i & -i)
        return s

    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x
            i += (i & -i)

    def reset(self):
        self.bit = [0] * (self.size + 1)


lo, hi = -1, 10 ** 9
BIT = BinaryIndexedTree(N + 1)
while hi - lo > 1:
    X = (hi + lo) // 2
    B = [(1 if a >= X else -1) for a in A]
    B = [0] + list(accumulate(B))
    B_sorted = {b: i for i, b in enumerate(sorted(B), start=1)}

    tmp = 0
    BIT.reset()
    for j, b in enumerate(B):
        tmp += BIT.sum(B_sorted[b])
        BIT.add(B_sorted[b], 1)

    if tmp >= ceil((N * (N + 1) / 2) / 2):
        lo = X
    else:
        hi = X

print(lo)

Submission status: Wrong Answer

Solution code:
def add(B,a,n):#リストに値を追加する関数
    x = a
    while x <= n:
        B[x] += 1
        x += x&(-x)

def sums(B,a):#a番目までの和
    x = a
    S = 0
    while x != 0:
        S += B[x]
        x -= x&(-x)
    return S


def invnumber(n, S):# #{(i,j)| i<j and S[i]<=S[j]}
    B = [0]*(n*2 + 1)
    invs = 0
    for i in range(n):
        s = S[i] + n
        invs += sums(B, s)
        add(B, s, n*2)
    invs += sums(B, n*2)
    return invs

N = int( input())
A = list( map( int, input().split()))
R = max(A)+1
L = 0
c = (N*(N+1)//2 + 1)//2
while R - L > 1:
    M = (R+L)//2
    S = [0]*N
    if A[0] >= M:
        S[0] = 1
    else:
        S[0] = -1
    for i in range(1,N):
        if A[i] >= M:
            S[i] = S[i-1] + 1
        else:
            S[i] = S[i-1] - 1
    if invnumber(N,S) >= c:
        L = M
    else:
        R = M
print(L)

Submission status: Wrong Answer

Solution code:
def add(B,a,n):#リストに値を追加する関数
    x = a
    while x <= n:
        B[x] += 1
        x += x&(-x)

def sums(B,a):#a番目までの和
    x = a
    S = 0
    while x != 0:
        S += B[x]
        x -= x&(-x)
    return S


def invnumber(n, S):# #{(i,j)| i<j and S[i]<=S[j]}
    B = [0]*(n*2 + 1)
    invs = 0
    for i in range(n):
        s = S[i] + n
        add(B, s, n*2)
        invs += sums(B, s)
    return invs

N = int( input())
A = list( map( int, input().split()))
R = max(A)+1
L = 0
c = (N*(N+1)//2 + 1)//2
while R - L > 1:
    M = (R+L)//2
    S = [0]*N
    if A[0] >= M:
        S[0] = 1
    else:
        S[0] = -1
    for i in range(1,N):
        if A[i] >= M:
            S[i] = S[i-1] + 1
        else:
            S[i] = S[i-1] - 1
    if invnumber(N,S) >= c:
        L = M
    else:
        R = M
print(L)

Submission status: Wrong Answer

Solution code:
def add(B,a,n):#リストに値を追加する関数
    x = a
    while x <= n:
        B[x] += 1
        x += x&(-x)

def sums(B,a):#a番目までの和
    x = a
    S = 0
    while x != 0:
        S += B[x]
        x -= x&(-x)
    return S


def invnumber(n, S):# #{(i,j)| i<j and S[i]<=S[j]}
    B = [0]*(n*2 + 1)
    invs = 0
    for i in range(n):
        s = S[i] + n
        add(B, s, n*2)
        invs += sums(B, s)
    return invs

N = int( input())
A = list( map( int, input().split()))
R = max(A)+1
L = min(A)
c = (N*(N+1)//2 + 1)//2

while R - L > 1:
    M = (R+L)//2
    S = [0]*N
    if A[0] >= M:
        S[0] = 1
    else:
        S[0] = -1
    for i in range(1,N):
        if A[i] >= M:
            S[i] = S[i-1] + 1
        else:
            S[i] = S[i-1] - 1
    if invnumber(N,S) >= c:
        L = M
    else:
        R = M
print(L)

Submission status: Wrong Answer

Solution code:
#from itertools import accumulate
def add(B,a,n):#リストに値を追加する関数
    x = a
    while x <= n:
        B[x] += 1
        x += x&(-x)

def sums(B,a):#a番目までの和
    x = a
    S = 0
    while x != 0:
        S += B[x]
        x -= x&(-x)
    return S


def invnumber(n, S):# #{(i,j)| i<j and S[i]<=S[j]}
    B = [0]*(n*2 + 1)
    invs = 0
    for i in range(n):
        s = S[i] + n
        invs += sums(B, s)
        add(B, s, n*2)
    return invs

N = int( input())
A = list( map( int, input().split()))
R = max(A)+1
L = min(A)
c = (N*(N-1)//2 + 1)//2

while R - L > 1:
    M = (R+L)//2
    S = [0]*N
    if A[0] >= M:
        S[0] = 1
    else:
        S[0] = -1
    for i in range(1,N):
        if A[i] >= M:
            S[i] = S[i-1] + 1
        else:
            S[i] = S[i-1] - 1
    if invnumber(N,S) >= c:
        L = M
    else:
        R = M
print(L)