[파이썬] 1038 감소하는 수 from itertools import combinations n = int(input()) li = [] for i in range(1, 11): for com in combinations(range(10), i): com = sorted(list(com), reverse=True) li.append(int("".join(map(str, com)))) li.sort() print(li[n] if len(li) > n else -1) 완탐 Algorithm/boj 2022.06.08
[파이썬] 17404 RGB거리 2 n = int(input()) board = [list(map(int, input().split())) for _ in range(n)] dp = [[int(1e6)] * 3 for _ in range(n)] ans = int(1e6) for i in range(3): dp[0] = [int(1e6)] * 3 dp[0][i] = board[0][i] for j in range(1, n): dp[j][0] = board[j][0] + min(dp[j - 1][1], dp[j - 1][2]) dp[j][1] = board[j][1] + min(dp[j - 1][0], dp[j - 1][2]) dp[j][2] = board[j][2] + min(dp[j - 1][0], dp[j - 1][1]) for j in.. Algorithm/boj 2022.06.05
[파이썬] 13305 주유소 n = int(input()) city = list(map(int, input().split())) cost = list(map(int, input().split())) ans, d = 0, 0 prev = cost.pop(0) for i in range(n - 1): d += city[i] if prev > cost[i]: ans += prev * d prev = cost[i] d = 0 print(ans + prev * d) 그리디 Algorithm/boj 2022.06.05
[파이썬] 17387 선분 교차 2 li = [list(map(int, input().split())) for _ in range(2)] a, b, c, d = li[0][:2], li[0][2:], li[1][:2], li[1][2:] def ccw(x1, y1, x2, y2, x3, y3): c = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) return 0 if c == 0 else c // abs(c) l1 = ccw(*a, *b, *c) * ccw(*a, *b, *d) l2 = ccw(*c, *d, *a) * ccw(*c, *d, *b) if not l1 and not l2: if a > b: a, b = b, a if c > d: c, d = d, c print(1 if a Algorithm/boj 2022.06.04
[파이썬] 16236 아기 상어 from collections import deque n = int(input()) board, start = [], [] dx = [-1, 0, 1, 0] dy = [0, -1, 0, 1] size, fish, count = 2, 0, 0 ans = 0 for i in range(n): li = list(map(int, input().split())) for j, l in enumerate(li): if l == 9: li[j] = 0 start = [i, j] elif l > 0: fish += 1 board.append(li) def bfs(x, y): global size, fish, count, ans visited = [[0] * n for _ in range(n)] li = [] m = 10.. Algorithm/boj 2022.06.03
[파이썬] 2166 다각형의 면적 n = int(input()) li = [list(map(int, input().split())) for _ in range(n)] li += [li[0]] ans = 0 for i in range(n): [x1, y1], [x2, y2] = li[i], li[i + 1] ans += x1 * y2 - x2 * y1 print(round(abs(ans) / 2, 1)) 기하 Algorithm/boj 2022.06.02
[파이썬] 1043 거짓말 n, m = map(int, input().split()) s = set(list(map(int, input().split()))[1:]) party = [set(list(map(int, input().split()))[1:]) for _ in range(m)] for _ in range(m): for p in party: if p & s: s |= p ans = 0 for p in party: if not p & s: ans += 1 print(ans) set Algorithm/boj 2022.05.31
[파이썬] 2638 치즈 from collections import deque n, m = map(int, input().split()) board, cheeze = [], deque() dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] for i in range(n): li = list(map(int, input().split())) for j in range(m): if li[j] == 1: cheeze.append([i, j]) board.append(li) def outer(): visited = [[0] * m for _ in range(n)] q = deque([[0, 0]]) while q: x, y = q.popleft() board[x][y] = -1 for i in range(4): nx, n.. Algorithm/boj 2022.05.31
[파이썬] 13172 Σ def power(num, t): if t == 1: return num % d elif t % 2: return num * power(num, t - 1) % d else: p = power(num, t // 2) return p * p % d m = int(input()) d = 1000000007 ans = 0 for _ in range(m): n, s = map(int, input().split()) ans += s * power(n, d - 2) % d print(ans % d) 분할정복 Algorithm/boj 2022.05.30
[파이썬] 2473 세 용액 n = int(input()) li = list(map(int, input().split())) fix, left, right = 0, 1, n - 1 ans = [3 * 10 ** 9 + 1, 0, 0, 0] li.sort() while fix < n: while left < right: f, l, r = li[fix], li[left], li[right] if abs(f + l + r) < ans[0]: ans = [abs(f + l + r), f, l, r] if f + l + r == 0: print(*ans[1:]) exit() elif f + l + r < 0: left += 1 else: right -= 1 fix += 1 left = fix + 1 right = n - 1 print(*an.. Algorithm/boj 2022.05.28