Submission #67790344


Source Code Expand

import sys
input = sys.stdin.readline

from heapq import heappop,heappush


T=int(input())
for tests in range(T):
    n=int(input())
    A=list(map(int,input().split()))
    K=[1]*n

    ANS=0
    H=[]

    for i in range(n):
        H.append((A[i],i))

    H.sort()

    LEFT=[i-1 for i in range(n)]
    RIGHT=[i+1 for i in range(n)]

    USE=[0]*n

    for tt in range(n-1):
        #print(A,USE,H,ANS)
        while H:
            x,ind=heappop(H)

            if USE[ind]==1:
                continue
            if A[ind]!=x:
                continue
            break

        p=1<<60
        m=1<<60

        if RIGHT[ind]<len(A):
            p=A[RIGHT[ind]]

        if LEFT[ind]>=0:
            m=A[LEFT[ind]]

        #print(ind)

        if p>m:
            x=A[ind]
            ko=K[ind]


            while x<m:
                if ko==1:
                    ANS+=(m-x)
                    break
                if ko%2==0:
                    ko//=2
                    x+=1
                else:
                    ko=(ko+1)//2
                    ANS+=1
                    x+=1


            #A[LEFT[ind]]+=1
            USE[ind]=1
            K[LEFT[ind]]+=ko

            if 0<=LEFT[ind]<n:
                RIGHT[LEFT[ind]]=RIGHT[ind]

            if 0<=RIGHT[ind]<n:
                LEFT[RIGHT[ind]]=LEFT[ind]


        else:
            x=A[ind]
            ko=K[ind]


            while x<p:
                if ko==1:
                    ANS+=(p-x)
                    break
                if ko%2==0:
                    ko//=2
                    x+=1
                else:
                    ko=(ko+1)//2
                    ANS+=1
                    x+=1


            #A[RIGHT[ind]]+=1
            USE[ind]=1
            K[RIGHT[ind]]+=ko

            if 0<=LEFT[ind]<n:
                RIGHT[LEFT[ind]]=RIGHT[ind]

            if 0<=RIGHT[ind]<n:
                LEFT[RIGHT[ind]]=LEFT[ind]

    #print(ANS,A,USE,K)

    for i in range(n):
        if USE[i]==0:
            score=K[i]

    while score>1:
        if score%2==0:
            score//=2
        else:
            ANS+=1
            score=(score+1)//2

    print(ANS)
        

    
            
        

Submission Info

Submission Time
Task A - Merge and Increment
User titia
Language Python (PyPy 3.10-v7.3.12)
Score 700
Code Size 2320 Byte
Status AC
Exec Time 551 ms
Memory 130892 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 64
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 03_sorted_00.txt, 03_sorted_01.txt, 03_sorted_02.txt, 03_sorted_03.txt, 03_sorted_04.txt, 03_sorted_05.txt, 04_almost_sorted_00.txt, 04_almost_sorted_01.txt, 04_almost_sorted_02.txt, 04_almost_sorted_03.txt, 04_almost_sorted_04.txt, 04_almost_sorted_05.txt, 05_same_00.txt, 05_same_01.txt, 05_same_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 07_hack_1_00.txt, 07_hack_1_01.txt, 07_hack_1_02.txt, 07_hack_1_03.txt, 07_hack_1_04.txt, 08_hack_2_00.txt, 08_hack_2_01.txt, 08_hack_2_02.txt, 08_hack_2_03.txt, 08_hack_2_04.txt, 09_hack_3_00.txt, 09_hack_3_01.txt, 09_hack_3_02.txt, 09_hack_3_03.txt, 09_hack_3_04.txt, 09_hack_3_05.txt, 09_hack_3_06.txt, 09_hack_3_07.txt, 09_hack_3_08.txt, 09_hack_3_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 57 ms 76364 KiB
01_small_00.txt AC 267 ms 87572 KiB
01_small_01.txt AC 381 ms 91492 KiB
01_small_02.txt AC 246 ms 86716 KiB
01_small_03.txt AC 171 ms 84944 KiB
01_small_04.txt AC 184 ms 84848 KiB
01_small_05.txt AC 217 ms 85284 KiB
01_small_06.txt AC 214 ms 85216 KiB
01_small_07.txt AC 226 ms 85176 KiB
01_small_08.txt AC 249 ms 85940 KiB
01_small_09.txt AC 159 ms 87512 KiB
01_small_10.txt AC 187 ms 86300 KiB
01_small_11.txt AC 246 ms 87840 KiB
01_small_12.txt AC 250 ms 88104 KiB
01_small_13.txt AC 277 ms 86424 KiB
01_small_14.txt AC 303 ms 86604 KiB
02_random_00.txt AC 440 ms 117128 KiB
02_random_01.txt AC 502 ms 114900 KiB
02_random_02.txt AC 421 ms 118764 KiB
02_random_03.txt AC 491 ms 115032 KiB
02_random_04.txt AC 486 ms 115556 KiB
02_random_05.txt AC 506 ms 114704 KiB
02_random_06.txt AC 335 ms 112520 KiB
02_random_07.txt AC 507 ms 114712 KiB
02_random_08.txt AC 335 ms 112188 KiB
02_random_09.txt AC 492 ms 115012 KiB
03_sorted_00.txt AC 183 ms 114884 KiB
03_sorted_01.txt AC 203 ms 115092 KiB
03_sorted_02.txt AC 200 ms 114720 KiB
03_sorted_03.txt AC 182 ms 114716 KiB
03_sorted_04.txt AC 202 ms 114820 KiB
03_sorted_05.txt AC 201 ms 115328 KiB
04_almost_sorted_00.txt AC 181 ms 115152 KiB
04_almost_sorted_01.txt AC 201 ms 114888 KiB
04_almost_sorted_02.txt AC 202 ms 114884 KiB
04_almost_sorted_03.txt AC 180 ms 114708 KiB
04_almost_sorted_04.txt AC 205 ms 115068 KiB
04_almost_sorted_05.txt AC 204 ms 114896 KiB
05_same_00.txt AC 183 ms 129780 KiB
05_same_01.txt AC 186 ms 115212 KiB
05_same_02.txt AC 189 ms 114944 KiB
06_corner_00.txt AC 275 ms 124400 KiB
06_corner_01.txt AC 186 ms 114892 KiB
06_corner_02.txt AC 186 ms 115040 KiB
07_hack_1_00.txt AC 227 ms 114772 KiB
07_hack_1_01.txt AC 208 ms 114936 KiB
07_hack_1_02.txt AC 221 ms 115208 KiB
07_hack_1_03.txt AC 241 ms 114884 KiB
07_hack_1_04.txt AC 207 ms 114960 KiB
08_hack_2_00.txt AC 337 ms 114868 KiB
08_hack_2_01.txt AC 343 ms 114980 KiB
08_hack_2_02.txt AC 338 ms 114768 KiB
08_hack_2_03.txt AC 342 ms 114980 KiB
08_hack_2_04.txt AC 327 ms 114944 KiB
09_hack_3_00.txt AC 179 ms 130008 KiB
09_hack_3_01.txt AC 182 ms 130028 KiB
09_hack_3_02.txt AC 382 ms 129796 KiB
09_hack_3_03.txt AC 372 ms 129808 KiB
09_hack_3_04.txt AC 463 ms 130156 KiB
09_hack_3_05.txt AC 480 ms 130452 KiB
09_hack_3_06.txt AC 519 ms 130516 KiB
09_hack_3_07.txt AC 489 ms 130520 KiB
09_hack_3_08.txt AC 551 ms 130892 KiB
09_hack_3_09.txt AC 535 ms 130876 KiB