Skip to content

1107 리모컨 #68

@srlee056

Description

@srlee056
Owner

48% 까지 pass했는데 오류 발생 이유를 모르겠음...

from sys import stdin

cur_channel = 100

input = stdin.readline

n = int(input().strip())
m = int(input().strip())

buttons = {i: [i - 1, i + 1] for i in range(0, 10)}
working_buttons = set(i for i in range(10))
broken_buttons = set()
if m > 0:
    broken_buttons = set(map(int, input().split()))
    working_buttons = working_buttons - broken_buttons

for button in broken_buttons:
    l, r = buttons[button]
    while r in broken_buttons and r != 10:
        r = buttons[r][1]
    while l in broken_buttons and l != -1:
        l = buttons[l][0]
    # print(button, l, r)
    buttons[button] = [l, r]
    if l != -1:
        buttons[l][1] = r
    if r != 10:
        buttons[r][0] = l


# print(buttons)

leng = len(str(n))

if n == cur_channel:
    print(0)
elif m == 0:
    print(min(abs(n - cur_channel), leng))
elif len(working_buttons) == 0:
    print(abs(n - cur_channel))
else:
    min_b, max_b = min(working_buttons), max(working_buttons)
    result = ""
    str_n = str(n)
    numbers = list(str_n)

    # print(numbers)
    for i, num in enumerate(numbers):
        n_ = str_n[i:]
        if num == "0":
            smaller = -1
        elif buttons[int(num)][0] == -1:
            smaller = int("0" + str(max_b) * (leng - i - 1))
            if smaller in broken_buttons:
                smaller = -1
        else:
            smaller = int(str(buttons[int(num)][0]) + str(max_b) * (leng - i - 1))

        if buttons[int(num)][1] == 10 and 1 not in broken_buttons:
            if 0 in broken_buttons:
                bigger = int("11" + str(min_b) * (leng - i - 1))
            else:
                bigger = int("10" + str(min_b) * (leng - i - 1))
        elif buttons[int(num)][1] == 10:
            bigger = -1
        else:
            bigger = int(str(buttons[int(num)][1]) + str(min_b) * (leng - i - 1))

        if i != 0:
            if len(n_) > len(str(smaller)):
                smaller = -1
            if len(n_) < len(str(bigger)):
                bigger = -1

        print("i", i, num)
        print(smaller, bigger)
        if int(num) in working_buttons:
            num1 = int(num + str(min_b) * (leng - i - 1))
            num2 = int(num + str(max_b) * (leng - i - 1))
            # print(num1, num2)
            if smaller != -1 and num1 - int(n_) >= int(n_) - smaller:
                # print(num, num1, n_, smaller)
                result += str(smaller)
                break
            elif bigger != -1 and int(n_) - num2 > bigger - int(n_):
                # print(num, bigger, n_, num2)
                result += str(bigger)
                break
            else:
                result += num
                # print("result", num, result)
        else:
            # print(n_, smaller, bigger)

            if smaller == -1 and bigger >= int(n_):
                result += "0" * (len(n_) - len(str(bigger))) + str(bigger)
            elif bigger == -1 and int(n_) >= smaller:
                result += "0" * (len(n_) - len(str(smaller))) + str(smaller)
            elif smaller != -1 and bigger != -1:
                if int(n_) - smaller <= bigger - int(n_):
                    result += "0" * (len(n_) - len(str(smaller))) + str(smaller)
                else:
                    result += "0" * (len(n_) - len(str(bigger))) + str(bigger)
            else:
                result = str(cur_channel)
            break
    # print(result)
    print(min(abs(n - cur_channel), len(str(int(result))) + abs(int(result) - n)))


"""
13퍼
input
99999
9
0 2 3 4 5 6 7 8 9
output:88893
answer:11118

반례로 문제 해결

"""

"""
28퍼

99999
8
2 3 4 5 6 7 8 9
output:88893
answer:7
100000(6) -(1)
"""

"""
38 퍼

41008
6   
0 1 2 3 5 9




4998
6 
0 2 3 5 8 9

"""


"""

48퍼



"""

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @srlee056

      Issue actions

        1107 리모컨 · Issue #68 · srlee056/algorithm-study