풍선 터뜨리기2346

LJM·2023년 1월 3일
0

백준풀기

목록 보기
10/259

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

ArrayList활용

터진풍선은 remove 하지 않고 9999 라고 임의로 값을 덮어씌우고 탐색할때 9999면 분기처리하도록함
다음 터질 풍선까지의 거리는 pang
pang의 값이 바뀌므로 pangcopy로 값을 복사해놓고 왼쪽이나 오른쪽 탐색 방향에 사용함

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main
{
    public static void main(String[] args)
    {
        FastReader fr = new FastReader();

        int ballooncnt = fr.nextInt();

        ArrayList<Integer> arrList = new ArrayList<>();

        for(int i = 0; i < ballooncnt; ++i)
        {
            arrList.add(fr.nextInt());
        }

        int none = 9999;
        int idx = 0;
        int pang = 0;
        int pangcopy = 0;

        StringBuilder sb = new StringBuilder();

        int NoneCnt = 0;
        while(NoneCnt < ballooncnt)
        {

            if(arrList.get(idx) == none)
            {
                if(pangcopy > 0)
                {
                    idx++;
                }
                else if(pangcopy < 0)
                {
                    idx--;
                }

            }
            else
            {
                if(pang == 0)
                {
                    pang = arrList.get(idx);
                    pangcopy = pang;
                    arrList.set(idx, none);
                    NoneCnt++;
                    if(NoneCnt == ballooncnt)
                    {
                        sb.append(idx+1);
                    }
                    else
                    {
                        sb.append((idx+1)+" ");
                    }
                }

                if(pangcopy > 0)
                {
                    idx++;
                    pang--;
                }
                else if(pangcopy < 0)
                {
                    idx--;
                    pang++;
                }

            }

            if(idx >= ballooncnt)
                idx = 0;

            if(idx < 0)
                idx = ballooncnt-1;
        }

        System.out.println(sb.toString());
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        FastReader()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next()
        {
            while(st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        String readLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

    }
}
profile
게임개발자 백엔드개발자

0개의 댓글