結果

問題 No.2182 KODOKU Stone
ユーザー Shirotsume
提出日時 2022-12-30 22:44:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 399 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,060 KB
実行使用メモリ 124,320 KB
最終ジャッジ日時 2024-11-25 21:18:04
合計ジャッジ時間 9,339 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353
from collections import deque
import bisect
import heapq
class Heap():
    def __init__(self, maxi = False):
        self.sum = 0
        self.al = []
        self.maxi = maxi
        self.len = 0

    def __len__(self):
        return len(self.al)

    def add(self, x):
        if self.maxi:
            heapq.heappush(self.al, -x)
        else:
            heapq.heappush(self.al, x)
        self.sum += x
    
    def pop(self):
        if self.maxi:
            now = heapq.heappop(self.al)
            self.sum += now
            return -now
        else:
            now = heapq.heappop(self.al)
            self.sum -= now
            return now

    def top(self):
        now = self.pop()
        self.add(now)
        return now
n = ii()

k = li()
a = []
for _ in range(n):
    _ = ii()
    a.append(list(sorted(li())))

def check(x):
	p = []
	q_max = 0
	for v in a:
		gt = len(v) - bisect.bisect_left(v, x)
		if gt < len(v):
			p.append(gt)
		elif q_max < gt:
			q_max = gt
	if not p:
		return True
	p.sort()
	que = Heap()
	for v in k[len(p):]:
		que.add(v)
	if(len(que) and q_max >= que.top()):
		return True

	for i in range(len(p) - 1, -1, -1):
		u = k[i] if len(que) == 0 else min(que.top(), k[i])
		if(p[i] >= u):
			return True
		elif(p[i] < u - 1):
			return False
		elif(len(que) and p[i] == que.top() - 1 and q_max >= k[i]):
			return True
		elif(i == 0):
			return False
		elif(len(que) == 0 or p[i] < que.top() - 1):
			continue
		que.pop()
		que.add(k[i])
	return True


A = []
for i in range(n):
    for v in a[i]:
        A.append(v)


A = list(set(A))
A.sort()
ok = 0
ng = len(A)
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if check(A[mid]):
        ok = mid
    else:
        ng = mid
print(A[ok])
0