링크 : https://www.acmicpc.net/problem/15787
15787번: 기차가 어둠을 헤치고 은하수를
입력의 첫째 줄에 기차의 수 N(1 ≤ N ≤ 100000)과 명령의 수 M(1 ≤ M ≤ 100000)가 주어진다. 이후 두 번째 줄부터 M+1번째 줄까지 각 줄에 명령이 주어진다.
www.acmicpc.net
- 문제
- 소요시간: 38분 40초



- 설계하기(접근방법)
1. 기차의 수와 명령어 수를 입력받는다
2. 명령어 구현
명령 1
입력 : 1 i x
i 번째 기차에 x번째 좌석에 태워라
-> i-1번째 인덱스를 1로 변경
명령 2
i 번쨰 기차의 x번째 좌석 승객을 하차시켜라
-> i-1 번째 인덱스를 0으로 변경
명령 3
i번재 기차에 0을 인덱스[0]에 insert하고
마지막 인덱스를 pop시킨다
명령 4
i번째 기차에 0을 append하고, 0번째 index를 pop시킨다
3. 명령 리스트 순회
명령 리스트를 순회하면서
각각의 명령을 순회한다
4. 리스트의 중복을 제거해준 후 len를 출력한다
- 코드(출력)
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
train_list = []
order_list = []
pass_list = []
for _ in range(n):
train_list.append([0 for _ in range(20)])
for _ in range(m):
order_list.append(list(map(int, input().split())))
def t1(n, m):
train_list[n-1][m-1] = 1
def t2(n, m):
train_list[n-1][m-1] = 0
def t3(n):
train_list[n-1].insert(0, 0)
train_list[n-1].pop()
def t4(n):
train_list[n-1].append(0)
del train_list[n-1][0]
for i in range(m):
if order_list[i][0] == 1:
t1(order_list[i][1], order_list[i][2])
if order_list[i][0] == 2:
t2(order_list[i][1], order_list[i][2])
if order_list[i][0] == 3:
t3(order_list[i][1])
if order_list[i][0] == 4:
t4(order_list[i][1])
for j in train_list:
if j not in pass_list:
pass_list.append(j)
print(len(pass_list))
- 얻어갈 부분
'알고리즘(백준) > 구현' 카테고리의 다른 글
| [알고리즘/구현] 16926번 : 배열 돌리기 - python (0) | 2023.05.09 |
|---|---|
| [알고리즘/구현] 2615번 : 오목 - python (0) | 2023.05.02 |
| [알고리즘/구현] 17276번 : 배열 돌리기 - python (0) | 2023.04.20 |
| [알고리즘/구현] 22858번 : 원상 복구(small) - python (0) | 2023.04.19 |
| [알고리즘/구현] 17413번 : 단어 뒤집기 2 - python (0) | 2023.04.18 |