728x90
728x90
728x90
import sys

resignation_day = sys.stdin.readline()

time_and_pay = [list(map(int, x.strip().split())) for x in sys.stdin.readlines()]

time_list = [x[0] for x in time_and_pay]
pay_list = [x[1] for x in time_and_pay]
cand = [0]

def go(time_list, pay_list, cur_ind, cumulative_pay):
    # 불가능
    if cur_ind >= len(time_list):
        return None

    # 정답
    if (cur_ind + time_list[cur_ind]) > len(time_list):
        if cand[0] < cumulative_pay:
            cand[0] = cumulative_pay
    if (cur_ind + time_list[cur_ind]) == len(time_list):
        if cand[0] < (cumulative_pay + pay_list[cur_ind]):
            cand[0] = cumulative_pay + pay_list[cur_ind]

    go(time_list, pay_list, cur_ind + time_list[cur_ind], cumulative_pay + pay_list[cur_ind])
    go(time_list, pay_list, cur_ind + 1, cumulative_pay)

    return cand[0]

print(go(time_list, pay_list, 0, 0))
  • 포인트
    • Dynamic programming
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][14500] 테트로미노  (0) 2022.05.08
[백준][13023] ABCDE  (0) 2022.05.08
[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
728x90
import sys

height, width = [int(x) for x in sys.stdin.readline().split()]

mapping = [list(map(int, x.split())) for x in sys.stdin.readlines()]
case_to_sub = {
    0: 2,
    1: 1,
    2: 8,
    3: 4,
    4: 4
}

def case_sub_to_available(case, sub, total_row, total_col):
    if case == 0 and sub == 0:
        return total_row, total_col - 3, 0, 0
    elif case == 0 and sub == 1:
        return total_row - 3, total_col, 0, 0

    elif case == 1 and sub == 0:
        return total_row - 1, total_col - 1, 0, 0

    elif case == 2 and sub == 0:
        return total_row - 2, total_col - 1, 0, 0
    elif case == 2 and sub == 1:
        return total_row - 1, total_col - 2, 0, 0
    elif case == 2 and sub == 2:
        return total_row - 2, total_col - 1, 0, 0
    elif case == 2 and sub == 3:
        return total_row - 1, total_col - 2, 1, 0

    elif case == 2 and sub == 4:
        return total_row - 2, total_col - 1, 2, 0
    elif case == 2 and sub == 5:
        return total_row - 1, total_col - 2, 0, 0
    elif case == 2 and sub == 6:
        return total_row - 2, total_col - 1, 0, 0
    elif case == 2 and sub == 7:
        return total_row - 1, total_col - 2, 0, 0

    elif case == 3 and sub == 0:
        return total_row - 2, total_col - 1, 0, 0
    elif case == 3 and sub == 1:
        return total_row - 1, total_col - 2, 1, 0

    elif case == 3 and sub == 2:
        return total_row - 2, total_col - 1, 1, 0
    elif case == 3 and sub == 3:
        return total_row - 1, total_col - 2, 0, 0

    elif case == 4 and sub == 0:
        return total_row - 1, total_col - 2, 0, 0
    elif case == 4 and sub == 1:
        return total_row - 2, total_col - 1, 1, 0
    elif case == 4 and sub == 2:
        return total_row - 1, total_col - 2, 1, 0
    elif case == 4 and sub == 3:
        return total_row - 2, total_col - 1, 0, 0

def get_val(case, sub, r, c, d):
    if case == 0 and sub == 0:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r][c + 3]
    elif case == 0 and sub == 1:
        return d[r][c] + d[r + 1][c] + d[r + 2][c] + d[r + 3][c]

    elif case == 1 and sub == 0:
        return d[r][c] + d[r + 1][c] + d[r][c + 1] + d[r + 1][c + 1]

    elif case == 2 and sub == 0:
        return d[r][c] + d[r + 1][c] + d[r + 2][c] + d[r + 2][c + 1]
    elif case == 2 and sub == 1:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c]
    elif case == 2 and sub == 2:
        return d[r][c] + d[r][c + 1] + d[r + 1][c + 1] + d[r + 2][c + 1]
    elif case == 2 and sub == 3:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r - 1][c + 2]

    elif case == 2 and sub == 4:
        return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r - 2][c + 1]
    elif case == 2 and sub == 5:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c + 2]
    elif case == 2 and sub == 6:
        return d[r][c] + d[r][c + 1] + d[r + 1][c] + d[r + 2][c]
    elif case == 2 and sub == 7:
        return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 1][c + 2]

    elif case == 3 and sub == 0:
        return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 2][c + 1]
    elif case == 3 and sub == 1:
        return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r - 1][c + 2]
    elif case == 3 and sub == 2:
        return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r + 1][c]
    elif case == 3 and sub == 3:
        return d[r][c] + d[r][c + 1] + d[r + 1][c + 1] + d[r + 1][c + 2]

    elif case == 4 and sub == 0:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c + 1]
    elif case == 4 and sub == 1:
        return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r + 1][c + 1]
    elif case == 4 and sub == 2:
        return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r - 1][c + 1]
    elif case == 4 and sub == 3:
        return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 2][c]

max_ = 0
for case in case_to_sub.keys():
    for sub in range(case_to_sub[case]):
        row_cnt, col_cnt, start_row, start_col = case_sub_to_available(case, sub, height, width)
        for r in range(start_row, start_row + row_cnt, 1):
            for c in range(start_col, start_col + col_cnt, 1):
                val = get_val(case, sub, r, c, mapping)
                if val > max_:
                    max_ = val
print(max_)
  • 포인트
    • Bruteforce
    • 모든 도형마다 가능한 좌우대칭 경우를 생성
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][14501] 퇴사  (0) 2022.05.08
[백준][13023] ABCDE  (0) 2022.05.08
[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
728x90
import sys

person_count, relation_count = [int(x) for x in sys.stdin.readline().strip().split()]

data = [list(map(int, x.strip().split())) for x in sys.stdin.readlines()]

relation = {k: [] for k in range(person_count)}

for x in data:
    relation[x[0]].append(x[1])
    relation[x[1]].append(x[0])

relation = {k: set(v) for k, v in relation.items()}

for k in relation.keys():
    for i in relation[k] - set([k]):
        for j in relation[i] - set([k, i]):
            for u in relation[j] - set([k, i, j]):
                if len(relation[u] - set([k, i, j, u])):
                    print(1)
                    exit()
print(0)
  • 포인트
    • 길이가 5인 path의 존재인 문제인데 길이가 5로 고정되어 네번의 for-loop로 해결
    • dfs로 길이가 5되는 것 찾아도 될 듯
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][14501] 퇴사  (0) 2022.05.08
[백준][14500] 테트로미노  (0) 2022.05.08
[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
728x90
import sys

sys.setrecursionlimit(99999)

vertex_count, edge_count = [int(x) for x in sys.stdin.readline().strip().split()]

edges = [list(map(int, each.strip().split())) for each in sys.stdin.readlines()]

adj_list = [[] for _ in range(vertex_count + 1)]
chk_list = [False] * (vertex_count + 1)
connected_components = []

for each in edges:
    adj_list[each[0]].append(each[1])
    adj_list[each[1]].append(each[0])

for v in range(1, len(adj_list)):
    adj_list[v] = sorted(list(set(adj_list[v])))

def dfs(vertex, connected_component):
    chk_list[vertex] = True
    connected_component.append(vertex)
    for neighbor in adj_list[vertex]:
        if not chk_list[neighbor]:
            dfs(neighbor, connected_component)

for vertex in range(1, len(adj_list)):
    if not chk_list[vertex]:
        connected_component = []
        dfs(vertex, connected_component)
        connected_components.append(connected_component)

print(len(connected_components))
  • 포인트
    • dfs
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][14500] 테트로미노  (0) 2022.05.08
[백준][13023] ABCDE  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
728x90
import sys

N = int(sys.stdin.readline())
data = list(map(int, sys.stdin.readline().split()))

def next_permutation(list_):
    get_i = 0
    for i in range(len(list_)-1):
        if list_[i] < list_[i+1]:
            get_i = i

    get_j = 0
    for j in range(get_i+1, len(list_), 1):
        if list_[get_i] < list_[j]:
            get_j = j

    if get_i == 0 and get_j == 0:
        return False
    else:
        list_[get_i], list_[get_j] = list_[get_j], list_[get_i]
        get_j = len(list_)-1
        while (get_i+1 < get_j):
            list_[get_i+1], list_[get_j] = list_[get_j], list_[get_i+1]
            get_i += 1; get_j -= 1
        return True

if next_permutation(data):
    res = [str(x) for x in data]
    res = ' '.join(res)
    print(res)
else:
    print(-1)
  • 포인트
    • next permutation은 증가조짐인 가장 오른쪽놈(i)와 (i)의 오른쪽놈들 중 (i)보다 큰놈 중 가장 작은 놈(=큰놈 중 가장 오른쪽 놈)(j)을 교체하는 것
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][13023] ABCDE  (0) 2022.05.08
[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
728x90
import sys

N = sys.stdin.readline()
data = list(map(int, sys.stdin.readline().split()))

data.sort()

def next_permutation(list):
    last_asc_idx = 0
    for i in range(len(list) - 1):
        if list[i] < list[i + 1]:
            last_asc_idx = i

    val_at_last_asc_idx = list[last_asc_idx]
    last_idx_larger_than_val_at_last_asc_idx = 0
    for j in range(last_asc_idx + 1, len(list), 1):
        if val_at_last_asc_idx < list[j]:
            last_idx_larger_than_val_at_last_asc_idx = j

    if last_asc_idx == 0 and last_idx_larger_than_val_at_last_asc_idx == 0:
        return False
    else:
        list[last_asc_idx], list[last_idx_larger_than_val_at_last_asc_idx] = \\
            list[last_idx_larger_than_val_at_last_asc_idx], list[last_asc_idx]
        last_idx_larger_than_val_at_last_asc_idx = len(list) - 1
        while last_asc_idx + 1 < last_idx_larger_than_val_at_last_asc_idx:
            list[last_asc_idx + 1], list[last_idx_larger_than_val_at_last_asc_idx] = \\
                list[last_idx_larger_than_val_at_last_asc_idx], list[last_asc_idx + 1]
            last_asc_idx += 1;
            last_idx_larger_than_val_at_last_asc_idx -= 1
        return True

def get_res(list):
    res = 0
    for i in range(len(list) - 1):
        if list[i] < list[i + 1]:
            res += list[i + 1] - list[i]
        else:
            res += list[i] - list[i + 1]
    return res

max_res = get_res(data)
while next_permutation(data):
    tmp_res = get_res(data)
    if max_res < tmp_res:
        max_res = tmp_res

print(max_res)
  • 포인트
    • bruteforce 파악
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
[백준][6603] 로또  (0) 2022.05.08
728x90
import sys

test_case_count = int(sys.stdin.readline())

def get_gcd(m, n):
    if n > m:
        m, n = n, m
    if n == 0:
        return m
    else:
        return get_gcd(n, m % n)

for i in range(test_case_count):
    numbers = [int(x) for x in sys.stdin.readline().split()][1:]
    res = 0
    for j in range(0, len(numbers) - 1, 1):
        for k in range(1, len(numbers) - j, 1):
            res += get_gcd(numbers[j], numbers[j + k])
    print(res)
  • 포인트
    • 쉬움
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][10972] 다음 순열  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
[백준][6603] 로또  (0) 2022.05.08
[백준][6588] 골드바흐의 추측  (0) 2022.05.08
728x90
import sys

test_case_count = int(sys.stdin.readline())
target_numbers = [int(x.strip()) for x in sys.stdin.readlines()]

def go(target, cumulative_sum):
    res = 0
    # 불가능한 경우
    if cumulative_sum > target:
        return 0

    # 정답을 찾은 경우
    if cumulative_sum == target:
        return 1

    # 계속 호출하는 경우
    if cumulative_sum < target:
        for i in [1, 2, 3]:
            res += go(target, cumulative_sum + i)
    return res

for target in target_numbers:
    print(go(target, 0))
  • 포인트
    • recursive
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][10819] 차이를 최대로  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
[백준][6603] 로또  (0) 2022.05.08
[백준][6588] 골드바흐의 추측  (0) 2022.05.08
[백준][4963] 섬의 개수  (0) 2022.05.08
728x90
import sys

data = [list(map(int, x.strip().split())) for x in sys.stdin.readlines()]

def go(current_list, remainder_list):
    # 로또 번호를 여섯개 이상 넣은 경우
    if len(current_list) > 6:
        return None

    # 여섯개 추첨
    if len(current_list) == 6:
        print(' '.join([str(x) for x in current_list]))

    # 아직 숫자가 모자르면
    if 0 <= len(current_list) < 6:
        # 남은 숫자가 있다면
        if len(remainder_list):
            for idx, val in enumerate(remainder_list):
                go(current_list + [val], remainder_list[(idx + 1):])

for ind, list_ in enumerate(data):
    if list_[0] == 0:
        exit()
    else:
        if ind != 0:
            print(' ')
        go([], list_[1:])
  • 포인트
    • 첫 번호을 기준으로 dynamic programming
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][9613] GCD 합  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
[백준][6588] 골드바흐의 추측  (0) 2022.05.08
[백준][4963] 섬의 개수  (0) 2022.05.08
[백준][2667] 단지 번호 붙이기  (0) 2022.05.08
728x90
import sys

data = [int(x.strip()) for x in sys.stdin.readlines()][:-1]

def prime_candidate_generator_less_than(n):
    # 3, 5, 7, 11, 13, 17, 19, ...
    yield 2
    yield 3
    candidate = 5
    increment = 2
    increment_changer = {2: 4, 4: 2}
    while candidate <= n:
        yield candidate
        candidate += increment
        increment = increment_changer[increment]

def get_primes_by_sieve_less_than(n):  # n >= 6
    prime_list = []
    sieve_list = [True] * (n + 1)
    for candidate in prime_candidate_generator_less_than(n):
        if sieve_list[candidate]:
            prime_list.append(candidate)
            for i in range(2, 1 + (n // candidate)):
                sieve_list[candidate * i] = False
    return prime_list, sieve_list

prime_list, sieve_list = get_primes_by_sieve_less_than(max(data))
for i in data:
    for j in prime_list:
        if sieve_list[i - j]:
            print("{0} = {1} + {2}".format(i, j, i - j))
            break
  • 포인트
    • 에라토스테네스의 체
    • 가장 큰 n에 대해서 체를 한번만 이용하기
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
[백준][6603] 로또  (0) 2022.05.08
[백준][4963] 섬의 개수  (0) 2022.05.08
[백준][2667] 단지 번호 붙이기  (0) 2022.05.08
[백준][2609] 최대공약수와 최소공배수  (0) 2022.05.08

+ Recent posts