結果

問題 No.1734 Decreasing Elements
ユーザー platinumplatinum
提出日時 2021-08-23 00:22:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,661 ms / 3,000 ms
コード長 6,592 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 13,312 KB
実行使用メモリ 64,428 KB
最終ジャッジ日時 2024-11-06 11:47:34
合計ジャッジ時間 42,086 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
16,256 KB
testcase_01 AC 43 ms
16,128 KB
testcase_02 AC 45 ms
16,256 KB
testcase_03 AC 44 ms
16,128 KB
testcase_04 AC 42 ms
16,128 KB
testcase_05 AC 42 ms
16,256 KB
testcase_06 AC 44 ms
16,128 KB
testcase_07 AC 1,403 ms
51,080 KB
testcase_08 AC 1,422 ms
50,440 KB
testcase_09 AC 1,439 ms
50,492 KB
testcase_10 AC 1,427 ms
51,356 KB
testcase_11 AC 1,752 ms
55,976 KB
testcase_12 AC 1,115 ms
41,548 KB
testcase_13 AC 1,700 ms
53,456 KB
testcase_14 AC 2,357 ms
62,640 KB
testcase_15 AC 2,004 ms
58,084 KB
testcase_16 AC 2,398 ms
62,636 KB
testcase_17 AC 2,388 ms
63,284 KB
testcase_18 AC 2,441 ms
63,924 KB
testcase_19 AC 2,661 ms
64,304 KB
testcase_20 AC 2,658 ms
64,300 KB
testcase_21 AC 2,142 ms
54,448 KB
testcase_22 AC 2,358 ms
61,228 KB
testcase_23 AC 2,156 ms
64,304 KB
testcase_24 AC 1,967 ms
64,300 KB
testcase_25 AC 2,167 ms
64,428 KB
testcase_26 AC 2,116 ms
63,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

#じゅっぴーさんAVL

##search(0,x):O(logN)
# xがある場合indexを、ない場合Noneを返す
def search(root,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return None
        else:
            return search(avl_left[root],key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return None
        else:
            return search(avl_right[root],key)
    return root


##end_lower/higher:search_lower/higherで使用
def end_lower(root):
    if avl_left[root] == None:
        return avl_key[root]
    else:
        return end_lower(avl_left[root])

def end_higher(root):
    if avl_right[root] == None:
        return avl_key[root]
    else:
        return end_higher(avl_right[root])


##search_lower(0,x,None):O(logN)
# xより小さいものの中で最も大きいものを出力。ない場合はNoneを出力
def search_lower(root,key,lower_key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return lower_key
        else:
            return search_lower(avl_left[root],key,lower_key)
    if avl_key[root] < key:
        lower_key = avl_key[root]
        if avl_right[root] == None:
            return lower_key
        else:
            return search_lower(avl_right[root],key,lower_key)
    
    # == 
    if avl_left[root] == None:
        return lower_key
    else:
        if lower_key == None:
            return end_higher(avl_left[root])
        else:
            return max(lower_key,end_higher(avl_left[root]))

##search_higher(0,x,None):O(logN)
# xより大きいものの中で最も小さいものを出力。ない場合はNoneを出力
def search_higher(root,key,higher_key):
    if avl_key[root] > key:
        higher_key = avl_key[root]
        if avl_left[root] == None:
            return higher_key
        else:
            return search_higher(avl_left[root],key,higher_key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return higher_key
        else:
            return search_higher(avl_right[root],key,higher_key)
    
    # == 
    if avl_right[root] == None:
        return higher_key
    else:
        if higher_key == None:
            return end_lower(avl_right[root])
        else:
            return min(higher_key,end_lower(avl_right[root]))



##Rotation,replace,insertx:insertで使用
def DoubleRightRotation(x):
    tl = avl_left[x]
    avl_left[x] = avl_right[avl_right[tl]]
    avl_right[avl_right[tl]] = x
    tlr = avl_right[tl]
    avl_right[tl] = avl_left[tlr]
    avl_left[tlr] = tl
    if balance[tlr] == -1:
        balance[avl_right[tlr]] = 1
        balance[avl_left[tlr]] = 0
    elif balance[tlr] == 1:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = -1
    else:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = 0
    balance[tlr] = 0
    return tlr

def DoubleLeftRotation(x):
    tr = avl_right[x]
    avl_right[x] = avl_left[avl_left[tr]]
    avl_left[avl_left[tr]] = x
    trl = avl_left[tr]
    avl_left[tr] = avl_right[trl]
    avl_right[trl] = tr
    if balance[trl] == 1:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = -1
    elif balance[trl] == -1:
        balance[avl_left[trl]] = 0
        balance[avl_right[trl]] = 1
    else:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = 0
    balance[trl] = 0
    return trl

def SingleLeftRotation(x):
    tr = avl_right[x]
    balance[tr] = 0
    balance[x] = 0
    avl_right[x] = avl_left[tr]
    avl_left[tr] = x
    return tr

def SingleRightRotation(x):
    tl = avl_left[x]
    balance[tl] = 0
    balance[x] = 0
    avl_left[x] = avl_right[tl]
    avl_right[tl] = x
    return tl

def replace(x,p,v):
    if avl_left[p] == x:
        avl_left[p] = v
    else:
        avl_right[p] = v

def insertx(root,p,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            if not insertx(avl_left[root],root,key):
                return False
        if balance[root] == 1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = -1
            return True
        else:
            if balance[avl_left[root]] == 1:
                replace(root,p,DoubleRightRotation(root))
            elif balance[avl_left[root]] == -1:
                replace(root,p,SingleRightRotation(root))
            return False
    if avl_key[root] < key:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            if not insertx(avl_right[root],root,key):
                return False
        if balance[root] == -1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = 1
            return True
        else:
            if balance[avl_right[root]] == -1:
                replace(root,p,DoubleLeftRotation(root))
            elif balance[avl_right[root]] == 1:
                replace(root,p,SingleLeftRotation(root))
            return False
    return False


##insert(0,x):O(logN)
#x追加
def insert(root,key):
    if key < avl_key[root]:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            insertx(avl_left[root],root,key)
    elif key > avl_key[root]:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            insertx(avl_right[root],root,key)
    else:
        pass
########################################################
##初期化(要素は一つ入れておく)

#avl_key:値,avl_left:左の子のindex,avl_right:右の子のindex
#balance[i]: {"Even":0,"Left":-1,"Right":1}

#avl_keyには初めに何か加えておく
n = int(input())
avl_key = [0]
avl_left = [None]*200001
avl_right = [None]*200001
balance = [0]*200001

A = map(int,input().split())
q = []
Max_A = 200001
heapq.heappush(q, (-Max_A, 0))
ans = n
for x in A :
    if search(0, x) != None :
        ans -= 1
        continue
    val = search_lower(0, x, None)
    dif = x - val
    res = []
    for i in range(len(q)) :
        p = heapq.heappop(q)
        ##print(p)
        if -p[0] <= dif :
            heapq.heappush(q, p)
            break
        res.append((-dif, p[1]))
        res.append((p[0] + dif, p[1] + dif))
        insert(0, p[1] + dif)
    for p in res :
        heapq.heappush(q, p)
print(ans)
0