-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
544 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
|
||
DIV = 1_000_000_000 | ||
|
||
N = int(sys.stdin.readline().rstrip()) | ||
|
||
dp = [[0] * 10 for _ in range(N+1)] | ||
answer = 0 | ||
|
||
for i in range(1,N+1): | ||
if i == 1: | ||
for k in range(1,10): | ||
dp[i][k] = 1 | ||
|
||
else: | ||
for num in range(10): | ||
if num == 0: | ||
dp[i][num] = dp[i-1][1]%DIV | ||
elif num == 9: | ||
dp[i][num] = dp[i-1][8]%DIV | ||
else: | ||
dp[i][num] = (dp[i-1][num-1] + dp[i-1][num+1])%DIV | ||
|
||
for k in range(10): | ||
answer += dp[N][k] | ||
|
||
print(answer%DIV) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
stdin = open(0) | ||
|
||
def input(): | ||
return stdin.readline().rstrip() | ||
|
||
N, M = map(int, input().split()) | ||
|
||
prices = {} | ||
|
||
for _ in range(N): | ||
item, price = input().split() | ||
prices[item] = int(price) | ||
|
||
recipes = [] | ||
|
||
for _ in range(M): | ||
target, formula = input().split('=') | ||
terms = formula.split('+') | ||
recipes.append([target, terms]) | ||
|
||
def updatePrice(target, terms): | ||
price = 0 | ||
|
||
for term in terms: | ||
count = int(term[0]) | ||
item = term[1:] | ||
|
||
if item not in prices: | ||
return | ||
|
||
price += count * prices[item] | ||
|
||
if target not in prices or prices[target] > price: | ||
prices[target] = price | ||
|
||
for _ in range(M): | ||
for recipe in recipes: | ||
updatePrice(recipe[0], recipe[1]) | ||
|
||
if 'LOVE' not in prices: | ||
print('-1') | ||
exit() | ||
|
||
answer = min(prices['LOVE'], 1000000001) | ||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# ์๋ฌผ์ ์ ์ค๊ฐ ๋ถ๋ถ์ด ๋ชจ๋ 1์ธ์ง ํ์ธ | ||
def is_valid(new_lock): | ||
length = len(new_lock) // 3 | ||
|
||
for r in range(length, length * 2): | ||
for c in range(length, length * 2): | ||
if new_lock[r][c] != 1: | ||
return False | ||
|
||
return True | ||
|
||
def solution(key, lock): | ||
n = len(lock) | ||
k = len(key) | ||
new_lock = [[0] * (n * 3) for _ in range(n * 3)] | ||
|
||
for r in range(n): | ||
for c in range(n): | ||
new_lock[r + n][c + n] = lock[r][c] | ||
|
||
for _ in range(4): | ||
rev_key = key[::-1] | ||
key = [] | ||
for c in range(k): | ||
row = [] | ||
for r in range(k): | ||
row.append(rev_key[r][c]) | ||
key.append(row) | ||
|
||
""" | ||
์ด์ ๋ฅผ ๋๋ฆฌ๋ ๋ก์ง์ ํ ์ค๋ก๋ ๊ตฌํ๊ฐ๋ฅ ํฉ๋๋ค | ||
key = [row for row in zip(*reversed(key))] | ||
""" | ||
|
||
for r in range(n * 2): | ||
for c in range(n * 2): | ||
# ์๋ฌผ์ ์ ์ด์ ๋ฅผ ๋ผ์ด๋ค | ||
for i in range(k): | ||
for j in range(k): | ||
new_lock[r + i][c + j] += key[i][j] | ||
|
||
# ์๋ฌผ์ ์ ์ด์ ๊ฐ ๋ฑ ๋ค์ด๊ฐ๋์ง ํ์ธ | ||
if is_valid(new_lock): | ||
return True | ||
|
||
# ์๋ฌผ์ ์์ ์ด์ ๋ฅผ ๋นผ์ ๋ณต๊ตฌ์ํจ๋ค | ||
for i in range(k): | ||
for j in range(k): | ||
new_lock[r + i][c + j] -= key[i][j] | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sys import * | ||
|
||
N = int(stdin.readline()) | ||
K = int(stdin.readline()) | ||
SENSORS = sorted(set(map(int, stdin.readline().split()))) | ||
|
||
if K >= N: | ||
print(0) | ||
exit() | ||
|
||
distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))] | ||
distances.sort(reverse=True) | ||
|
||
result = 0 | ||
for i in range(K - 1, len(distances)): | ||
result += distances[i] | ||
|
||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from heapq import * | ||
from sys import * | ||
|
||
C, R = map(int, stdin.readline().split()) | ||
arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)] | ||
|
||
drc = ((1,0),(-1,0),(0,1),(0,-1)) | ||
visited = [[False] * C for _ in range(R)] | ||
heap = [(0, 0, 0)] | ||
target_r = R - 1 | ||
target_c = C - 1 | ||
|
||
while heap: | ||
cost, r, c = heappop(heap) | ||
|
||
if r == target_r and c == target_c: | ||
print(cost) | ||
break | ||
if visited[r][c]: | ||
continue | ||
|
||
visited[r][c] = True | ||
|
||
for dr, dc in drc: | ||
nr = r + dr | ||
nc = c + dc | ||
|
||
if not 0 <= nr < R or not 0 <= nc < C: | ||
continue | ||
if visited[nr][nc]: | ||
continue | ||
|
||
heappush(heap, (cost + arr[nr][nc], nr, nc)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from heapq import * | ||
from collections import * | ||
|
||
def solution(jobs): | ||
jobs = deque(sorted(jobs)) | ||
jobs_num = len(jobs) | ||
|
||
curr_time = wait_time = 0 | ||
heap = [] | ||
|
||
while heap or jobs: | ||
while jobs and jobs[0][0] <= curr_time: | ||
requested_time, duration = jobs.popleft() | ||
heappush(heap, (duration, requested_time)) | ||
|
||
if heap: | ||
duration, requested_time = heappop(heap) | ||
|
||
curr_time += duration | ||
wait_time += curr_time - requested_time | ||
else: | ||
curr_time += 1 | ||
|
||
return wait_time // jobs_num |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import sys | ||
from collections import deque, defaultdict | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
|
||
H, W = map(int, input().split()) | ||
building = [["." for _ in range(W+2)]] | ||
door_info = defaultdict(set) | ||
keys_i_have = set() | ||
|
||
for row in range(H): | ||
_input = "." | ||
_input += input() | ||
_input += "." | ||
|
||
for col in range(W+2): | ||
if _input[col] not in ("*", ".", "$") and _input[col].isupper(): | ||
door_info[_input[col]].add((row+1, col)) | ||
|
||
building.append(list(_input)) | ||
|
||
building.append(["." for _ in range(W+2)]) | ||
|
||
keys_info = input() | ||
if keys_info != "0": | ||
keys_i_have.update(set(keys_info)) | ||
|
||
answer = 0 | ||
visited = set() | ||
locked_doors_to_access = set() | ||
|
||
deq = deque([(0, 0)]) | ||
while deq: | ||
now_row, now_col = deq.popleft() | ||
|
||
for dir in range(4): | ||
new_row = now_row + dy[dir] | ||
new_col = now_col + dx[dir] | ||
|
||
if new_row < 0 or new_row >= H+2: continue | ||
if new_col < 0 or new_col >= W+2: continue | ||
if (new_row, new_col) in visited: continue | ||
if building[new_row][new_col] == "*": continue | ||
|
||
# print(now_row, now_col,building[new_row][new_col]) | ||
# print(locked_doors_to_access) | ||
# print(keys_i_have) | ||
# print() | ||
|
||
if building[new_row][new_col] == "$": | ||
answer += 1 | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
continue | ||
|
||
# ๋ฌธ์ ๋ง๋ฌ์ ๊ฒฝ์ฐ, ์ด ๋ ๊น์ง ์ป์ ์ด์ ๋ก ์ด ์ ์๋ ์ง ํ์ธํจ. ์๋ ๊ฒฝ์ฐ ์ ๊ทผํ ์ ์๋ ๋ฌธ ๋ชฉ๋ก์ ์ถ๊ฐ | ||
if building[new_row][new_col].isalpha() and building[new_row][new_col].isupper(): | ||
|
||
# ์ด์ ๋ฅผ ์ด๋ฏธ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ | ||
if building[new_row][new_col].lower() in keys_i_have: | ||
building[new_row][new_col] = "." | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
# ์ด์ ๊ฐ ์์ด์ ๋ฌธ์ ๋ชป ์ด ๊ฒฝ์ฐ ์ ๊ทผํ ์ ์๋ ๋ฌธ ๋ชฉ๋ก์ ์ถ๊ฐ | ||
else: locked_doors_to_access.add((new_row, new_col)) | ||
|
||
continue | ||
|
||
# ์ด์ ๋ฅผ ํ๋ํ์ ๊ฒฝ์ฐ, ์ด ๋ ๊น์ง ๋ง๋ ๋ฌธ๋ค ์ค์ ์ด ์ ์๋ ๊ฒ๋ค์ queue์ ๋ฃ์ | ||
if building[new_row][new_col].isalpha() and building[new_row][new_col].islower(): | ||
keys_i_have.add(building[new_row][new_col]) | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
for can_open_row, can_open_col in door_info[building[new_row][new_col].upper()]: | ||
if (can_open_row, can_open_col) in locked_doors_to_access: | ||
building[can_open_row][can_open_col] = "." | ||
visited.add((can_open_row, can_open_col)) | ||
deq.append((can_open_row, can_open_col)) | ||
locked_doors_to_access.discard((can_open_row, can_open_col)) | ||
|
||
continue | ||
|
||
# ๋น ๊ณต๊ฐ์ผ ๊ฒฝ์ฐ, ๊ทธ๋ฅ ์ง๋๊ฐ | ||
if building[new_row][new_col] == ".": | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from collections import defaultdict, deque | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
T = int(input()) | ||
for _ in range(T): | ||
N, M, K = map(int, input().split()) | ||
|
||
costs = [[int(1e9) for _ in range(M+1)] for _ in range(N+1)] | ||
costs[1][0] = 0 | ||
|
||
graph = [[] for _ in range(N+1)] | ||
for _ in range(K): | ||
start, destination, cost, duration = map(int,input().split()) | ||
graph[start].append((destination, cost, duration)) | ||
|
||
# print(edges) | ||
|
||
for cost in range(M+1): | ||
for city in range(1, N): | ||
if costs[city][cost] == int(1e9): continue | ||
|
||
for now_destination, now_cost, now_duration in graph[city]: | ||
|
||
if now_cost + cost <= M and costs[now_destination][cost + now_cost] > costs[city][cost] + now_duration: | ||
costs[now_destination][cost + now_cost] = costs[city][cost] + now_duration | ||
|
||
result = min(costs[N]) | ||
print(result) if result != int(1e9) else print("Poor KCM") |
Oops, something went wrong.