-
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
12 changed files
with
332 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sys | ||
sys.setrecursionlimit(10**6) | ||
from collections import deque | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
arr = [[i for i in input()]for _ in range(N)] | ||
visited = [[0] * N for _ in range(N)] | ||
direc = [(1,0), (-1,0), (0,1), (0,-1)] | ||
result = [0, 0] | ||
|
||
def bfs(x, y, color): | ||
q = deque([(x,y)]) | ||
visited[x][y] = 1 | ||
while q: | ||
cx, cy = q.popleft() | ||
for dx, dy in direc: | ||
nx = dx + cx | ||
ny = dy + cy | ||
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny] and arr[nx][ny] == color: | ||
q.append((nx, ny)) | ||
visited[nx][ny] = 1 | ||
|
||
|
||
for i in range(N): | ||
for j in range(N): | ||
if not visited[i][j]: | ||
bfs(i,j,arr[i][j]) | ||
result[0] += 1 | ||
|
||
visited = [[0] * N for _ in range(N)] | ||
|
||
for i in range(N): | ||
for j in range(N): | ||
if arr[i][j] == "R": | ||
arr[i][j] = "G" | ||
|
||
for i in range(N): | ||
for j in range(N): | ||
if not visited[i][j]: | ||
bfs(i,j,arr[i][j]) | ||
result[1] += 1 | ||
|
||
|
||
print(result[0], result[1], end=" ") |
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,39 @@ | ||
import sys | ||
sys.setrecursionlimit(10**6) | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
eggs = [list(map(int, input().split())) for _ in range(N)] | ||
|
||
def break_egg(idx): | ||
cnt = 0 | ||
|
||
# idx๊ฐ ๋์ด๊ฐ ๊ฒฝ์ฐ | ||
if idx == N: | ||
for i in range(N): | ||
if eggs[i][0] <= 0: | ||
cnt += 1 | ||
return cnt | ||
|
||
if eggs[idx][0] <= 0: # ๋ค๊ณ ์๋ ๊ณ๋์ด ๊นจ์ง ๊ฒฝ์ฐ | ||
return break_egg(idx+1) | ||
|
||
broken = False | ||
for j in range(N): # ๊ณ๋ ๊นจ๊ธฐ | ||
if j != idx and eggs[j][0] > 0: | ||
broken = True | ||
eggs[idx][0] -= eggs[j][1] | ||
eggs[j][0] -= eggs[idx][1] | ||
|
||
cnt = max(cnt, break_egg(idx+1)) | ||
|
||
eggs[idx][0] += eggs[j][1] | ||
eggs[j][0] += eggs[idx][1] | ||
|
||
if not broken: | ||
return break_egg(idx+1) | ||
|
||
return cnt | ||
|
||
print(break_egg(0)) |
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
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,40 @@ | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
private const val INF = 987_654_321 | ||
fun main() { | ||
val br = BufferedReader(InputStreamReader(System.`in`)) | ||
val (n, m) = br.readLine().split(" ").map { it.toInt() } | ||
val graph = Array(n + 1) { i -> | ||
Array(n + 1) { j -> if (i == j) 0 else INF } | ||
} | ||
|
||
repeat(m) { | ||
val (a, b) = br.readLine().split(" ").map { it.toInt() } | ||
graph[a][b] = 1 | ||
graph[b][a] = 1 | ||
} | ||
|
||
for (k in 1..n) { | ||
for (i in 1..n) { | ||
for (j in 1..n) { | ||
if (graph[i][j] > graph[i][k] + graph[k][j]) { | ||
graph[i][j] = graph[i][k] + graph[k][j] | ||
} | ||
} | ||
} | ||
} | ||
|
||
var min = INF | ||
var answer = -1 | ||
|
||
for (i in 1..n) { | ||
val sum = (1..n).sumOf { j -> graph[i][j] } | ||
if (min > sum) { | ||
min = sum | ||
answer = i | ||
} | ||
} | ||
|
||
println(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,73 @@ | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import kotlin.math.max | ||
|
||
val t1 = listOf(0 to 0, 0 to 1, 0 to 2, 0 to 3) | ||
val t2 = listOf(0 to 0, 0 to 1, 1 to 0, 1 to 1) | ||
val t3 = listOf(0 to 0, 1 to 0, 2 to 0, 2 to 1) | ||
val t4 = listOf(0 to 0, 1 to 0, 1 to 1, 2 to 1) | ||
val t5 = listOf(0 to 0, 0 to 1, 0 to 2, 1 to 1) | ||
val tetrominos = listOf(t1, t2, t3, t4, t5) | ||
|
||
private var n: Int = 0 | ||
private var m: Int = 0 | ||
private lateinit var board: Array<Array<Int>> | ||
fun main() { | ||
val br = BufferedReader(InputStreamReader(System.`in`)) | ||
val input = br.readLine().split(" ").map { it.toInt() } | ||
n = input[0] | ||
m = input[1] | ||
|
||
board = Array(n) { Array(m) { 0 } } | ||
// ์ซ์๋ฅผ ๋ณด๋์ ์ธํ | ||
repeat(n) { | ||
val line = br.readLine().split(" ").map { it.toInt() } | ||
line.forEachIndexed { index, value -> board[it][index] = value } | ||
} | ||
|
||
var maxTetromino = 0 | ||
|
||
// 0,0 ๋ถํฐ n-1, m-1 ๊น์ง ํ์ | ||
for (x in 0 until n) { | ||
for (y in 0 until m) { | ||
tetrominos.forEach { tetromino -> | ||
// ๊ทธ๋๋ก | ||
maxTetromino = max(maxTetromino, sumAllDirection(x, y, tetromino)) | ||
// ๋ค์ง์ด์ | ||
maxTetromino = max(maxTetromino, sumAllDirection(x, y, tetromino.reverseTetromino())) | ||
} | ||
} | ||
} | ||
println(maxTetromino) | ||
} | ||
|
||
// 90๋์ฉ ํ์ ์์ผ ์ต๋๊ฐ return | ||
private fun sumAllDirection(x: Int, y: Int, tetromino: List<Pair<Int, Int>>): Int { | ||
var maxTetromino = 0 | ||
var nowTetromino = tetromino | ||
repeat(4) { | ||
maxTetromino = max(maxTetromino, sumTetromino(x, y, nowTetromino)) | ||
nowTetromino = nowTetromino.translate() | ||
} | ||
return maxTetromino | ||
} | ||
|
||
// board ์ tetromino ์ซ์ ํฉ ๊ณ์ฐ | ||
private fun sumTetromino(x: Int, y: Int, tetromino: List<Pair<Int, Int>>): Int { | ||
var sum = 0 | ||
for (move in tetromino) { | ||
val nowX = x + move.first | ||
val nowY = y + move.second | ||
if (nowX >= n || nowY >= m || nowX < 0 || nowY < 0) { | ||
return -1 | ||
} | ||
sum += board[nowX][nowY] | ||
} | ||
return sum | ||
} | ||
|
||
// tetromino ๋ฅผ 90๋ ํ์ | ||
private fun List<Pair<Int, Int>>.translate() = map { it.second to -1 * it.first } | ||
|
||
// tetromino ๋ฅผ ๋ค์ง๊ธฐ | ||
private fun List<Pair<Int, Int>>.reverseTetromino() = map { it.first to (it.second * -1) } |
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,47 @@ | ||
from heapq import * | ||
import sys | ||
|
||
input = sys.stdin.readline | ||
|
||
t = int(input()) | ||
|
||
for _ in range(t): | ||
q = int(input()) | ||
|
||
# ์ต์ํ๋ง ์ง์ํด์ ์ต๋, ์ต์ ๋ง๋ค์ด์ฃผ๊ธฐ | ||
min_heap = [] | ||
max_heap = [] | ||
nums = [False] * q # ๋๊ธฐํ๋ฅผ ์ํ ๋ถ๋ถ | ||
|
||
for i in range(q): | ||
k = list(input().split()) | ||
iNum = int(k[1]) | ||
|
||
# ์ ๋ ฅ ๋ถ๋ถ | ||
if k[0] == "I" : | ||
heappush(min_heap, (iNum,i)) # ๋๊ธฐํ๋ฅผ ์ํ i๋ ํํ๋ก ๋ฃ์ด์ฃผ๊ธฐ | ||
heappush(max_heap, (-iNum, i)) | ||
nums[i] = True | ||
elif iNum == 1: | ||
while max_heap and not nums[max_heap[0][1]]: | ||
heappop(max_heap) | ||
if max_heap: # max_heap์์ ์์ ์ ์ ๊ฑฐ | ||
nums[max_heap[0][1]] = False | ||
heappop(max_heap) | ||
else: | ||
while min_heap and not nums[min_heap[0][1]]: # ์ด๋ฏธ ์ญ์ ๋ ๋ ธ๋์ธ ๊ฒฝ์ฐ ์ญ์ ๋์ง ์์ ๋ ธ๋ ๋์ฌ๋๊น์ง ๋ชจ๋ ๋ฒ๋ฆผ | ||
heappop(min_heap) | ||
if min_heap: # min_heap์์ ์์ ์(์ญ์ ๋ ธ๋) ์ ๊ฑฐ | ||
nums[min_heap[0][1]] = False | ||
heappop(min_heap) | ||
# print(min_heap, max_heap, nums, k) | ||
# ๋ชจ๋ ์ฐ์ฐ ํ ๋๊ธฐํ ๋์ง ์์ ๋ ธ๋ ์ฒ๋ฆฌ | ||
while min_heap and not nums[min_heap[0][1]]: | ||
heappop(min_heap) | ||
while max_heap and not nums[max_heap[0][1]]: | ||
heappop(max_heap) | ||
|
||
if max_heap and min_heap : | ||
print(-max_heap[0][0], min_heap[0][0]) | ||
else : | ||
print("EMPTY") |
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,8 @@ | ||
def solution(citations): | ||
citations.sort(reverse=True) | ||
|
||
for i in range(len(citations)): | ||
if(citations[i] < i+1): | ||
return i | ||
|
||
return len(citations) |
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,27 @@ | ||
import sys | ||
|
||
sys.setrecursionlimit(10**6) | ||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
in_order = list(map(int,input().split())) | ||
post_order = list(map(int,input().split())) | ||
position = [0 for _ in range(N+1)] | ||
|
||
for idx, element in enumerate(in_order): | ||
position[element] = idx | ||
|
||
def divide_and_conquer(in_start, in_end, post_start, post_end): | ||
if in_start > in_end or post_start > post_end: return | ||
|
||
in_order_root = post_order[post_end] | ||
print(in_order_root, end=" ") | ||
|
||
root_idx = position[in_order_root] | ||
|
||
divide_and_conquer(in_start, root_idx-1, post_start, post_start+(root_idx - in_start) -1) | ||
divide_and_conquer(root_idx+1, in_end, post_start+(root_idx - in_start), post_end-1) | ||
|
||
return | ||
|
||
divide_and_conquer(0, N-1, 0, N-1) |
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,43 @@ | ||
from heapq import * | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N, M, T = map(int, input().split()) | ||
board = [] | ||
|
||
for _ in range(N): | ||
board.append(input().split()) | ||
|
||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
heap = [(0, 0, 0)] | ||
answer = int(1e9) | ||
visited = {(0,0),} | ||
while heap: | ||
now_time, now_row, now_col = heappop(heap) | ||
|
||
if now_row == N-1 and now_col == M-1: | ||
answer = min(answer, now_time) | ||
break | ||
|
||
if now_time >= T: continue | ||
|
||
for dir in range(4): | ||
new_row = now_row + dy[dir] | ||
new_col = now_col + dx[dir] | ||
|
||
if new_row < 0 or new_row >= N: continue | ||
if new_col < 0 or new_col >= M: continue | ||
if (new_row, new_col) in visited: continue | ||
|
||
if board[new_row][new_col] == "2": | ||
heappush(heap, (now_time+1, new_row, new_col)) | ||
answer = min(answer, (now_time+1+(N-1-new_row)+(M-1-new_col))) | ||
|
||
elif board[new_row][new_col] == "0": | ||
heappush(heap, (now_time+1, new_row, new_col)) | ||
|
||
visited.add((new_row, new_col)) | ||
|
||
print(answer) if answer <= T else print("Fail") |