ν°μ€ν 리 λ·°
π©π»π» λ¬Έμ
βπ» νμ΄
π¨ Go
// https://www.acmicpc.net/problem/1021
// λ±μ νμ©νμ¬ νλ₯Ό νμ μν€λ λ¬Έμ
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, m int
fmt.Fscanln(reader, &n, &m)
var q []queue
for i := 0; i < n; i++ {
tmp := queue{}
tmp.curPos = i + 1
tmp.initPos = i + 1
q = append(q, tmp)
}
var selectPos []int
for i := 0; i < m; i++ {
var pos int
fmt.Fscanf(reader, "%d ", &pos)
selectPos = append(selectPos, pos)
}
var count int
for len(selectPos) > 0 && len(q) > 0 {
for i := 0; i < len(q); i++ {
if q[i].initPos == selectPos[0] {
if i < len(q)-i {
count += i
q = moveLeft(q, i)
} else {
count += (len(q) - i)
q = moveRight(q, len(q)-i)
}
q = q[1:]
selectPos = selectPos[1:]
break
}
}
}
fmt.Fprintln(writer, count)
}
func moveLeft(q []queue, interval int) []queue {
q = append(q[interval:], q[0:interval]...)
return q
}
func moveRight(q []queue, interval int) []queue {
q = append(q[len(q)-interval:len(q)], q[0:len(q)-interval]...)
return q
}
type queue struct {
curPos int
initPos int
}
π¨ Python3
# https://www.acmicpc.net/problem/1021
# λ±μ νμ©νμ¬ νλ₯Ό νμ μν€λ λ¬Έμ
import sys
class Queue:
def __init__(self, cur_pos, init_pos):
self.cur_pos = cur_pos
self.init_pos = init_pos
def move_left(q, interval):
result = []
result.extend(q[interval:])
result.extend(q[0:interval])
return result
def move_right(q, interval):
result = []
result.extend(q[len(q)-interval:len(q)])
result.extend(q[0:len(q)-interval])
return result
if __name__ == "__main__":
n, m = list(map(int, sys.stdin.readline().split()))
q = []
for i in range(n):
q.append(Queue(i+1, i+1))
select_pos = list(map(int, sys.stdin.readline().split()))
count = 0
while len(select_pos) > 0 and len(q) > 0:
for i in range(len(q)):
if q[i].init_pos == select_pos[0]:
if i < len(q)-i:
count += i
q = move_left(q, i)
else:
count += (len(q) - i)
q = move_right(q, len(q)-i)
q = q[1:]
select_pos = select_pos[1:]
break
print(count)
728x90
'dev > algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ / 10816λ² / μ«μ μΉ΄λ 2 [Go][Python3] (0) | 2020.11.19 |
---|---|
BOJ / 1920λ² / μ μ°ΎκΈ° [Go][Python3] (0) | 2020.11.17 |
BOJ / 10866λ² / λ± [Go][Python3] (0) | 2020.11.13 |
BOJ / 1966λ² / νλ¦°ν° ν [Go][Python3] (0) | 2020.11.12 |
BOJ / 11866λ² / μμΈνΈμ€ λ¬Έμ 0 [Go][Python3] (0) | 2020.11.11 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ λ¬λ¦° λκΈ
TAG
- μ΄λΆνμ
- μκ°κ΅μ²΄
- AWS
- λ§₯λΆ
- λͺ½κ³ λλΉ
- ballet
- dp
- dfs
- λ§₯λΆ μ κ·Έλ μ΄λ
- λ°±μ€
- λ§₯λΆνλ‘
- ν΄μ맡
- BFS
- Golang
- μκ³ λ¦¬μ¦
- MongoDB
- Algorithm
- λ°λ
- ν
- Macbook pro 2012 mid 13
- λΈλ£¨νΈν¬μ€
- java
- λΆν μ 볡
- python3
- μλ°
- baekjoon
- BOJ
- νλ‘μ΄λμμ¬
- μ€ν
- go
- Total
- Today
- Yesterday