結果

問題 No.1734 Decreasing Elements
ユーザー platinumplatinum
提出日時 2021-08-23 00:23:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,160 ms / 3,000 ms
コード長 6,592 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 148,752 KB
最終ジャッジ日時 2024-11-06 11:48:08
合計ジャッジ時間 31,821 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
58,460 KB
testcase_01 AC 49 ms
58,836 KB
testcase_02 AC 50 ms
59,972 KB
testcase_03 AC 49 ms
59,528 KB
testcase_04 AC 49 ms
59,320 KB
testcase_05 AC 50 ms
59,584 KB
testcase_06 AC 51 ms
59,984 KB
testcase_07 AC 1,245 ms
137,508 KB
testcase_08 AC 1,423 ms
138,912 KB
testcase_09 AC 1,346 ms
138,880 KB
testcase_10 AC 1,286 ms
137,228 KB
testcase_11 AC 1,415 ms
142,540 KB
testcase_12 AC 1,052 ms
119,140 KB
testcase_13 AC 1,478 ms
137,944 KB
testcase_14 AC 2,160 ms
148,752 KB
testcase_15 AC 1,594 ms
141,968 KB
testcase_16 AC 1,976 ms
147,752 KB
testcase_17 AC 2,054 ms
145,416 KB
testcase_18 AC 2,080 ms
146,896 KB
testcase_19 AC 1,186 ms
129,196 KB
testcase_20 AC 1,112 ms
128,684 KB
testcase_21 AC 1,129 ms
122,896 KB
testcase_22 AC 1,218 ms
132,740 KB
testcase_23 AC 1,353 ms
129,344 KB
testcase_24 AC 1,467 ms
129,688 KB
testcase_25 AC 1,658 ms
141,088 KB
testcase_26 AC 1,723 ms
140,164 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