Notice
Recent Posts
Recent Comments
05-17 21:28
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

Byeol Lo

N2346 - 풍선 터뜨리기 본문

Algorithm/Stack & Queue

N2346 - 풍선 터뜨리기

알 수 없는 사용자 2023. 12. 9. 17:10

DoublyLinkedList를 문제에 적합하게 필요한 요구사항만 구현하도록 하는게 큰 관건이였다. inner class를 static으로 두고 super class가 이를 사용해 구현을 할 수 있다. 더 생각하면 Node의 setNext와 setPrev를 없앨 수 있을거 같은데, 그건 추후에 하도록 하겠다.(수정 완료)

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;

public class Main {
    static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static final StringBuffer SB = new StringBuffer();
    static StringTokenizer st;

    public static void main(String[] args) throws IOException{
        DoublyLinkedList<ArrayList<Integer>> dll = new DoublyLinkedList<>();
        input(dll);
        solution(dll);
        BR.close();
    }

    private static void input(DoublyLinkedList<ArrayList<Integer>> dll) throws IOException{
        int index = 1;
        BR.readLine();
        st = new StringTokenizer(BR.readLine());
        while(st.hasMoreTokens()) {
            dll.add(new ArrayList<>(Arrays.asList(
                    index,
                    Integer.parseInt(st.nextToken()))
            ));
            index++;
        }
    }

    private static void solution(DoublyLinkedList<ArrayList<Integer>> dll) {
        while(dll.size() != 0) {
            dll.remove();
            SB.append(dll.value().get(0)).append(" ");
            dll.offset(dll.value().get(1));
        }
        System.out.println(SB);
    }
}

class DoublyLinkedList<T> {
    int size = 0;
    Node<T> first;
    Node<T> curNode;
    Node<T> last;

    DoublyLinkedList() {
        first = last = curNode = null;
    }

    void add(T value) {
        if(first == null) first = last = curNode = new Node<>(value);
        else last = new Node<>(last, value, first);
        size++;
    }

    T value() {
        return curNode.value;
    }

    void offset(int n) {
        while(n != 0) {
            if (n > 0) {
                curNode = curNode.next;
                n--;
            } else {
                curNode = curNode.prev;
                n++;
            }
        }
    }

    void remove() {
        curNode.next.prev = curNode.prev;
        curNode.prev.next = curNode.next;
        size--;
    }

    int size() {
        return size;
    }

    static class Node<T> {
        Node<T> prev;
        Node<T> next;
        T value;

        Node(T data) {
            this.value = data;
            this.prev = this.next = this;
        }

        Node(Node<T> prev, T value, Node<T> next) {
            this(value);
            this.prev = prev;
            this.next = next;
            next.prev = prev.next = this;
        }
    }
}

 

'Algorithm > Stack & Queue' 카테고리의 다른 글

N24511 - queuestack  (0) 2023.12.09
N28279 - 덱 2  (1) 2023.12.08
N12789 - 도키도키 간식드리미  (0) 2023.12.08
N28278 - 스택 2  (0) 2023.12.08
Comments