Problem p03202 (python)

Fixed submission:
import random
input()
A = [int(_) for _ in input().split()]
A = [A[0]] + [j for i, j in zip(A, A[1:]) if i >= j]
N = len(A)


def cut(array, index):
    if index < 1:
        return []
    if index <= array[0][0]:
        return [(index, array[0][1])]
    for _ in range(len(array)-1, 0, -1):
        if array[_ - 1][0] < index:
            return array[:_] + [(index, array[_][1])]


def is_possible(K):
    dp = [(A[0], 0)]
    for a in A[1:]:
        if a <= dp[-1][0]:
            dp = cut(dp, a)
        else:
            dp += [(a, 0)]
        is_added = False
        for j in range(len(dp) - 1, -1, -1):
            if dp[j][1] < K - 1:
                dp = cut(dp, dp[j][0] - 1) + [(dp[j][0], dp[j][1] + 1)]
                if dp[-1][0] < a:
                    dp += [(a, 0)]
                is_added = True
                break
        if not is_added:
            return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
input()
A = [int(_) for _ in input().split()]
A = [A[0]] + [j for i, j in zip(A, A[1:]) if i >= j]
N = len(A)


def cut(array, index):
    if index < 1:
        return []
    for _ in range(len(array)):
        if index <= array[_][0]:
            return array[:_] + [(index, array[_][1])]


def is_possible(K):
    dp = [(A[0], 0)]
    for a in A[1:]:
        dp = cut(dp, a)
        is_added = False
        for j in range(len(dp) - 1, -1, -1):
            if dp[j][1] < K - 1:
                dp = cut(dp, dp[j][0] - 1) + [(dp[j][0], dp[j][1] + 1)]
                if dp[-1][0] < a:
                    dp += [(a, 0)]
                is_added = True
                break
        if not is_added:
            return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
input()
A = [int(_) for _ in input().split()]
A = [A[0]] + [j for i, j in zip(A, A[1:]) if i >= j]
N = len(A)


def cut(array, index):
    for _ in range(len(array)):
        if index < 1:
            return []
        elif index <= array[_][0]:
            return array[:_] + [(index, array[_][1])]


def is_possible(K):
    dp = [(A[0], 0)]
    for a in A[1:]:
        dp = cut(dp, a)
        is_added = False
        for j in range(len(dp) - 1, -1, -1):
            if dp[j][1] < K - 1:
                dp = cut(dp, dp[j][0] - 1) + [(dp[j][0], dp[j][1] + 1)]
                if dp[-1][0] < a:
                    dp += [(a, 0)]
                is_added = True
                break
        if not is_added:
            return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
input()
A = [int(_) for _ in input().split()]
A = [A[0]] + [j for i, j in zip(A, A[1:]) if i >= j]
N = len(A)


def is_possible(K):
    dp = [0] * A[0]
    for i in range(1, N):
        dp = dp[:A[i]]
        while True:
            if dp[-1] < K - 1:
                dp[-1] += 1
                break
            else:
                dp.pop()
            if not dp:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split() if int(_) < 19 else 18]
N = len(A)
for _ in range(3):
    flag = True
    B = [A[0]]
    for i in range(N - 1):
        if A[i] >= A[i + 1]:
            B += [A[i + 1]]
        else:
            flag = False
    A = B
    N = len(A)
    if flag:
        break

if N == 1:
    print(1)
    exit()


def is_possible(K):
    dp = [0] * 19
    for i in range(N - 1):
        if B[i] >= B[i + 1]:
            dp = dp[: B[i + 1]] + [0] * (19 - B[i + 1])
            flag = True
            for j in range(B[i + 1] - 1, -1, -1):
                if dp[j] < K - 1:
                    dp[j] += 1
                    flag = False
                    break
                else:
                    dp[j] = 0
            if flag:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split() if int(_) < 19]
N = len(A)
while True:
    flag = True
    B = [A[0]]
    for i in range(N - 1):
        if A[i] >= A[i + 1]:
            B += [A[i + 1]]
        else:
            flag = False
    A = B
    N = len(A)
    if flag:
        break

if N == 1:
    print(1)
    exit()


def is_possible(K):
    dp = [0] * 19
    for i in range(N - 1):
        if B[i] >= B[i + 1]:
            dp = dp[: B[i + 1]] + [0] * (19 - B[i + 1])
            flag = True
            for j in range(B[i + 1] - 1, -1, -1):
                if dp[j] < K - 1:
                    dp[j] += 1
                    flag = False
                    break
                else:
                    dp[j] = 0
            if flag:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split()]  # if int(_) < 19]
N = len(A)
while True:
    flag = True
    B = [A[0]]
    for i in range(N - 1):
        if A[i] >= A[i + 1]:
            B += [A[i + 1]]
        else:
            flag = False
    A = B.copy()
    N = len(A)
    if flag:
        break

if N == 1:
    print(1)
    exit()


def is_possible(K):
    dp = [0] * 19
    for i in range(N - 1):
        if B[i] >= B[i + 1]:
            dp = dp[: B[i + 1]] + [0] * (19 - B[i + 1])
            flag = True
            for j in range(B[i + 1] - 1, -1, -1):
                if dp[j] < K - 1:
                    dp[j] += 1
                    flag = False
                    break
                else:
                    dp[j] = 0
            if flag:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split() if int(_) < 19]
N = len(A)
while True:
    flag = True
    B = [A[0]]
    for i in range(N - 1):
        if A[i] >= A[i + 1]:
            B += [A[i + 1]]
        else:
            flag = False
    A = B.copy()
    N = len(A)
    if flag:
        break

if N == 1:
    print(1)
    exit()


def is_possible(K):
    dp = [0] * 19
    for i in range(N - 1):
        if B[i] >= B[i + 1]:
            dp = dp[: B[i + 1]] + [0] * (19 - B[i + 1])
            flag = True
            for j in range(B[i + 1] - 1, -1, -1):
                if dp[j] < K - 1:
                    dp[j] += 1
                    flag = False
                    break
                else:
                    dp[j] = 0
            if flag:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [0] + [int(_) for _ in input().split()]
B = [0]
if N == 1:
    print(1)
    exit()
is_monotonic_increasing = A[0] < A[1]
is_monotonic_decreasing = A[0] > A[1]
for i in range(1, N):
    if is_monotonic_increasing:
        is_monotonic_increasing &= A[i] < A[i + 1]
    elif is_monotonic_decreasing:
        is_monotonic_decreasing &= A[i] > A[i + 1]
    B += [min(A[i + 1], 18)]
if is_monotonic_increasing:
    print(1)
    exit()
if is_monotonic_decreasing:
    print(2)
    exit()


def is_possible(K):
    dp = [0] * 18
    for i in range(N):
        if B[i] >= B[i + 1]:
            dp = dp[:B[i + 1]] + [0] * (19 - B[i + 1])
            flag = True
            for j in range(B[i + 1] - 1, -1, -1):
                if dp[j] < K - 1:
                    dp[j] += 1
                    flag = False
                    break
                else:
                    dp[j] = 0
            if flag:
                return False
    return True


def bis(x, y):
    if y == x + 1:
        return y
    elif is_possible((x + y) // 2):
        return bis(x, (x + y) // 2)
    else:
        return bis((x + y) // 2, y)


print(bis(0, N))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split()]
dp = []
ans = 1
for a in A:
    N = len(dp)
    if N < a:
        dp += [1] * (a - N)
    else:
        flag = True
        for i in range(a, -1, -1):
            if dp[i] < ans:
                dp = dp[:i] + [dp[i] + 1] + [1] * (a - i - 1)
                flag = False
                break
        if flag:
            ans += 1
            dp = dp[:a - 1] + [ans]
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split()]
dp = []
ans = 1
for a in A:
    N = len(dp)
    if N < a:
        dp += [0] * (a - N)
    else:
        flag = True
        for i in range(a - 1, -1, -1):
            if dp[i] < ans:
                dp = dp[:i] + [dp[i] + 1] + [0] * (a - i - 1)
                flag = False
                break
        if flag:
            ans += 1
            dp = [ans] + [0] * (a - 1)
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split()]
dp = []
ans = 1
for a in A:
    N = len(dp)
    if N < a:
        dp += [0] * (a - N)
    else:
        flag = True
        for i in range(N - 1, -1, -1):
            if dp[i] < ans:
                dp[i] += 1
                flag = False
                break
        if flag:
            ans += 1
            dp = [ans] + [0] * (a - 1)
print(ans)