/images/jg_02.jpg

BEAKJOON 1021, 5430, 10866

1021_회전하는 큐

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys
from collections import deque

N, M = map(int, sys.stdin.readline().split())
arr = deque([i for i in range(1, N+1)])
poplist = deque(list(map(int, sys.stdin.readline().split())))

count = 0
while poplist:
    try:
        if arr[0] == poplist[0]:
            arr.popleft()
            poplist.popleft()
        L = len(arr)
        a = abs(arr.index(arr[0]) - arr.index(poplist[0]))
        b = L - a
        if a < b:
            arr.rotate(-a)
            count += a
        elif a >= b:
            arr.rotate(b)
            count += b
    except IndexError:
        break

print(count)

5430_AC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
from collections import deque

for T in range(int(sys.stdin.readline().rstrip())):
    P = sys.stdin.readline().rstrip()
    N = int(sys.stdin.readline().rstrip())
    ## 다양하게 input 받는 거 고민하기 좋다.
    arr = deque(list(sys.stdin.readline().rstrip()[1:-1].split(",")))

    if N == 0 or len(arr) == 0:
        arr = deque([])

    reverse = False
    error = False
    for i in P:
        if i == "R":
            reverse =  not reverse
        elif i == "D":
            if len(arr) == 0:
                error = True
                break
            if reverse:
                arr.pop()
            else:
                arr.popleft()
    if error:
        print("error")
    else:
        if reverse:
            arr.reverse()
        print("[", end="") 
        print(",".join(arr), end="") 
        print("]")


    ## reverse() 여러번 돌리면 시간초과 뜬다. 
    # flag = True
    # for i in P:
    #     if i == "R":
    #         arr.reverse()
    #     else:
    #         if len(arr) != 0:
    #             arr.popleft()
    #         else:
    #             print("error")
    #             flag = False
    #             break
    
    # if flag:
    #     if len(arr) != 0:

    #         print("[", end="") 
    #         print(",".join(arr), end="") 
    #         print("]")
    #     else:
    #         print('error')

BEAKJOON 18258, 2164, 11866, 1966

18258_큐 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
from collections import deque
# import time

N = int(sys.stdin.readline().rstrip())
# start = time.time()  # 시작 시간 저장

q = deque()

for _ in range(N):
    command = sys.stdin.readline().split() 
    if command[0] == "push":
        q.append(command[1])
    elif command[0] == "pop":
        if q:
            print(q.popleft())
        else:
            print(-1)
    elif command[0] == "size":
        print(len(q))
    elif command[0] == "empty":
        if not q:
            print(1)
        else:
            print(0)
    elif command[0] == "front":
        if q:
            print(q[0])
        else:
            print(-1)
    elif command[0] == "back":
        if q:
            print(q[len(q)-1])
        else:
            print(-1)

# print("time :", time.time() - start)  # 현재시각 - 시작시간 = 실행 시간


# q[len(q)-1] VS q[-1] 속도 비교
# q[len(q)-1] 이 더 빠름.

BEAKJOON 4949, 10828, 1874, 17298

4949_균형잡힌 세상

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys

while True:
    text = sys.stdin.readline().rstrip()
    if text == ".":
        break
    stack =[]
    result = "yes"
    for i in text:
        if i.isalpha() or i == " ":
            continue
        elif i == "(" or i == "[":
            stack.append(i)
        elif i == ")":
            if not stack or stack[-1] != "(":
                result = "no"
                break
            else:
                stack.pop()
        elif i == "]":
            if not stack or stack[-1] != "[":
                result = "no"
                break
            else:
                stack.pop()

    if not stack:
        print(result)
    else:
        print('no')

BEAKJOON 1037, 1934, 2609, 5086

1037_약수

1
2
3
4
5
6
7
8
import sys

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

Max = max(arr)
Min = min(arr)
print(Max*Min)

1934_최소공배수

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import sys

def GCD(a, b):
    while b != 0:
        m = a % b
        a = b
        b = m
    return a

for T in range(int(sys.stdin.readline().rstrip())):
    A, B = map(int, sys.stdin.readline().split())

    gcd = GCD(A, B)
    a = A // gcd
    b = B // gcd
    print(a*b*gcd)

2609_최대공약수와 최소공배수

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import sys

def GCD(a, b):  #최대공약수(유클리드 호제법)
    while b != 0:
        m = a % b
        a = b
        b = m
    return a
    

A, B = map(int, sys.stdin.readline().split())

gcd = GCD(A, B)

a = A // gcd
b = B // gcd            

print(gcd)
print(a*b*gcd)

BEAKJOON 1931, 11399, 1541, 13305

1931_회의실 배정

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import sys

N = int(sys.stdin.readline().rstrip())

arr = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
schedule = sorted(arr, key=lambda x: (x[1], x[0]))

count, end = 0, 0
for s, f in schedule:
    if s >= end:
        count += 1
        end = f

print(count)

# 왠지 정렬을 잘하면 계산이 쉬울 것 같아서. 
# 시작시간과 종료시간을 정렬해 계산했는데
# 1. 종료시간, 2. 시작시간 순위로 정렬해야 했다.

SW Expert Academy_D4 1226, 1227, 1231, 5643

D4_1226_미로1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
dx = [0,1, 0, -1]
dy = [1,0, -1, 0 ]

def IsSafe(x, y):
    if 0<= x < 16 and 0 <= y < 16:
        return True

def DFS(x, y):
    global result
    visited[x][y] = 1
    if arr[x][y] == 3:
        result = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if IsSafe(nx, ny) and visited[nx][ny] == 0 and arr[nx][ny] != 1:
            DFS(nx, ny)
            

def Find_Start():
    for i in range(16):
        for j in range(16):
            if arr[i][j] == 2:
                return i, j


for T in range(10):
    t = input()
    arr = [list(map(int, input())) for _ in range(16)]
    visited = [[0 for _ in range(16)] for _ in range(16)]

    x, y = Find_Start()
    result = 0
    DFS(x, y)
    
    print(f'#{T+1} {result}')


# 재귀를 사용할 시 return은 불편한 점이 많다.
# 하여 특정 조건에서의 값이 필요한 경우.
# 변수를 설정해서 그 값을 바꾸는 편이 간편하다. 
# 다만 런타임에 조심해야 한다.