BaekJoon 10866, 덱 구현

이번 문제는 덱을 구현하는 문제를 풀어봤습니다.

https://www.acmicpc.net/problem/10866

문제는 위에서 확인 가능합니다.

덱을 구현하는 것은 크게 어려운 점이 없었지만,

push를 구현할 때, pointer를 조심해야 한다는 것을 깨달았습니다.



 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
import java.io.*;
import java.util.ArrayList;

public class B1021 {
    static int N, M, count = 0, first;
    static String in, ins[];
    static ArrayList<Integer> numbers = new ArrayList<>();
    static ArrayList<Integer> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        in = br.readLine();
        N = Integer.parseInt(in.split(" ")[0]);
        M = Integer.parseInt(in.split(" ")[1]);
        ins = br.readLine().split(" ");
        for (int i = 0; i < M; i++) {
            numbers.add(Integer.parseInt(ins[i]));
        }
        for (int i = 1; i <= N; i++) {
            list.add(i);
        }
        boolean isPoll = true;
        int ans = 0;
        while (!numbers.isEmpty()) {
            if (isPoll) {
                count = 0;
                first = list.get(0);
            }
            if (numbers.get(0).equals(list.get(0))) {
                list.remove(0);numbers.remove(0);
                ans += (count > list.size() / 2) ? list.size() - count + 1 : count;
                isPoll = true;
            }else {
                leftRotate();count++;
                isPoll = false;
            }
        }
        bw.write(ans + "\n");
        bw.flush();bw.close();

    }
    public static void leftRotate() {
        int t = list.remove(0);
        list.add(t);
    }
}


이 블로그의 인기 게시물

웹툰 무료로 볼 수 있는 사이트

BackJoon 1011, Fly me to the alpha centauri, 규칙 찾기 문제

BaekJoon 6591, 이항 쇼다운 조합문제