Problem p03202 (python)

Fixed submission:
import sys
input = sys.stdin.readline
import heapq
from collections import defaultdict

N=int(input())
A=[0]+list(map(int,input().split()))

def Nextstring(nowlength,nextlength):
    if nextlength>nowlength:
        while -USELIST[0]>nowlength:
            dis=-heapq.heappop(USELIST)
            del NOWSTR[dis]
        nowlength=nextlength

    else:
        while -USELIST[0]>nextlength:
            dis=-heapq.heappop(USELIST)
            del NOWSTR[dis]

        if NOWSTR[nextlength]<mid-1:
            
            if NOWSTR[nextlength]==0:
                NOWSTR[nextlength]+=1
                heapq.heappush(USELIST,-nextlength)
                nowlength=nextlength
            else:
                NOWSTR[nextlength]+=1
                nowlength=nextlength

        else:
            klength=nextlength
            while NOWSTR[klength]==mid-1:
                if klength in NOWSTR:
                    del NOWSTR[klength]
                klength-=1
                if mid!=1:
                    dis=-heapq.heappop(USELIST)
                    if dis in NOWSTR:
                        del NOWSTR[dis]

                if klength==0:
                    return -1

            if NOWSTR[klength]==0:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

            else:
                NOWSTR[klength]+=1
                nowlength=nextlength

    return 0

for i in range(N):
    if A[i]>=A[i+1]:
        MIN=2
        break
else:
    print(1)
    sys.exit()

from collections import Counter
MAX=max(Counter(A).values())+1

while MIN!=MAX:
    mid=(MIN+MAX)//2
    nowlength=0
    NOWSTR=defaultdict(int)
    USELIST=[10**6]

    for i in range(N):

        if Nextstring(A[i],A[i+1])==-1:
            MIN=mid+1
            break
        #print(mid,NOWSTR,USELIST)
    else:
        MAX=mid

print(MIN)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import heapq
from collections import defaultdict

N=int(input())
A=[0]+list(map(int,input().split()))

def Nextstring(nowlength,nextlength):
    if nextlength>nowlength:
        while -USELIST[0]>nowlength:
            dis=-heapq.heappop(USELIST)
            del NOWSTR[dis]
        nowlength=nextlength

    else:
        while -USELIST[0]>nextlength:
            dis=-heapq.heappop(USELIST)
            del NOWSTR[dis]

        if NOWSTR[nextlength]<mid-1:
            
            if NOWSTR[nextlength]==0:
                NOWSTR[nextlength]+=1
                heapq.heappush(USELIST,-nextlength)
                nowlength=nextlength
            else:
                NOWSTR[nextlength]+=1
                nowlength=nextlength

        else:
            klength=nextlength
            while NOWSTR[klength]==mid-1:
                NOWSTR[klength]=0
                klength-=1
                if mid!=1:
                    dis=-heapq.heappop(USELIST)
                    del NOWSTR[dis]

                if klength==0:
                    return -1

            if NOWSTR[klength]==0:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

            else:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

    return 0

MIN=1
from collections import Counter
MAX=max(Counter(A).values())+1

while MIN!=MAX:
    mid=(MIN+MAX)//2
    nowlength=0
    NOWSTR=defaultdict(int)
    USELIST=[10**6]

    for i in range(N):

        if Nextstring(A[i],A[i+1])==-1:
            MIN=mid+1
            break
        #print(mid,NOWSTR,USELIST)
    else:
        MAX=mid

print(MIN)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import heapq

N=int(input())
A=[0]+list(map(int,input().split()))

def Nextstring(nowlength,nextlength):
    if nextlength>nowlength:
        while -USELIST[0]>nowlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0
        nowlength=nextlength

    else:
        while -USELIST[0]>nextlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0

        if NOWSTR[nextlength]<mid-1:
            
            if NOWSTR[nextlength]==0:
                NOWSTR[nextlength]+=1
                heapq.heappush(USELIST,-nextlength)
                nowlength=nextlength
            else:
                NOWSTR[nextlength]+=1
                nowlength=nextlength

        else:
            klength=nextlength
            while NOWSTR[klength]==mid-1:
                NOWSTR[klength]=0
                klength-=1
                if mid!=1:
                    dis=-heapq.heappop(USELIST)
                    NOWSTR[dis]=0

                if klength==0:
                    return -1

            if NOWSTR[klength]==0:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

            else:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

    return 0

MIN=1
MAX=len(A)+1

while MIN!=MAX:
    mid=(MIN+MAX)//2
    nowlength=0
    NOWSTR=[0]*(max(A)+1)
    USELIST=[10**6]

    for i in range(N):

        if Nextstring(A[i],A[i+1])==-1:
            MIN=mid+1
            break
        #print(mid,NOWSTR,USELIST)
    else:
        MAX=mid

print(MIN)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import heapq

N=int(input())
A=[0]+list(map(int,input().split()))

def Nextstring(nowlength,nextlength):
    if nextlength>nowlength:
        while -USELIST[0]>nowlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0
        nowlength=nextlength

    else:
        while -USELIST[0]>nextlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0

        if NOWSTR[nextlength]<mid-1:
            
            if NOWSTR[nextlength]==0:
                NOWSTR[nextlength]+=1
                heapq.heappush(USELIST,-nextlength)
                nowlength=nextlength
            else:
                NOWSTR[nextlength]+=1
                nowlength=nextlength

        else:
            klength=nextlength
            while NOWSTR[klength]==mid-1:
                NOWSTR[klength]=0
                klength-=1
                if mid!=1:
                    dis=-heapq.heappop(USELIST)
                    NOWSTR[dis]=0

                if klength==0:
                    return -1

            if NOWSTR[klength]==0:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

            else:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

    return 0

MIN=1
MAX=len(A)+1

while MIN!=MAX:
    mid=(MIN+MAX)//2
    nowlength=0
    NOWSTR=[0]*(max(A)+1)
    USELIST=[10**6]

    for i in range(N):

        if Nextstring(A[i],A[i+1])==-1:
            MIN=mid+1
            break
        #print(mid,NOWSTR,USELIST)
    else:
        MAX=mid

print(MIN)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import heapq

N=int(input())
A=[0]+list(map(int,input().split()))

def Nextstring(nowlength,nextlength):
    if nextlength>nowlength:
        while -USELIST[0]>nowlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0
        nowlength=nextlength

    else:
        while -USELIST[0]>nextlength:
            dis=-heapq.heappop(USELIST)
            NOWSTR[dis]=0

        if NOWSTR[nextlength]<mid-1:
            
            if NOWSTR[nextlength]==0:
                NOWSTR[nextlength]+=1
                heapq.heappush(USELIST,-nextlength)
                nowlength=nextlength
            else:
                NOWSTR[nextlength]+=1
                nowlength=nextlength

        else:
            klength=nextlength
            while NOWSTR[klength]==mid-1:
                NOWSTR[klength]=0
                klength-=1
                if mid!=1:
                    dis=-heapq.heappop(USELIST)
                    NOWSTR[dis]=0

                if klength==0:
                    return -1

            if NOWSTR[klength]==0:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

            else:
                NOWSTR[klength]+=1
                heapq.heappush(USELIST,-klength)
                nowlength=nextlength

    return 0

MIN=1
MAX=max(A)

while MIN!=MAX:
    mid=(MIN+MAX)//2
    nowlength=0
    NOWSTR=[0]*(max(A)+1)
    USELIST=[10**6]

    for i in range(N):

        if Nextstring(A[i],A[i+1])==-1:
            MIN=mid+1
            break
        #print(mid,NOWSTR,USELIST)
    else:
        MAX=mid

print(MIN)

Buggy submission (verdict: Runtime Error):
import sys

N=int(input())
A=list(map(int,input().split()))

for i in range(1,N):
    if A[i]<=A[i-1]:
        break
else:
    print(1)
    sys.exit()

kind=2
ketadict=dict()
ketadict[A[i]]=kind

for i in range(i+1,N):
    #print(i,ketadict,kind)
    if A[i]>A[i-1]:
        ketadict[A[i]]=1
        continue

    if ketadict.get(A[i],1)!=kind:
        ketadict[A[i]]=ketadict.get(A[i],1)+1
        continue

    if ketadict.get(A[i],1)==kind:
        x=A[i]
        while ketadict.get(x,1)==kind:
            ketadict[x]=1
            x-=1
        if x!=0:
            ketadict[x]=ketadict.get(x,1)+1
            for j in ketadict.keys():
                if j>x:
                    del ketadict[j]
        else:
            kind+=1
            ketadict=dict()
            ketadict[1]=kind
    
print(max(ketadict.values()))