22942데이터 체커

LJM·2023년 1월 6일
0

백준풀기

목록 보기
16/259

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

x 축위에 있으므로 원이 아니라 x축위의 막대기 간에 겹치는것을 확인하면 된다는 생각을 하는것까지는 좋았는데
막대기들의 중심을 기준으로 왼쪽부터 오른쪽으로 비교한다고 고민하니 답이 안나왔다

결국 다른사람들의 풀이를 찾아보고 그래도 바로 이해를 못하고 있다가 막대기의 왼쪽을 기준으로 고민해보니 풀을 수 있을거 같아서 시도해봤더니 해결되었다. 기준을 막대기의 왼쪽으로 두고 정렬을 하고 차례대로 비교하면 된다. 비교하는 부분을 정밀하게 겹치는 경우까지 고민해서 풀었다.
스택을 사용하지 않고 풀었다

import java.io.*;
import java.util.*;

class Round
{
    public int center;
    public int radius;
    public int left;
    public int right;

    public Round(int center, int radius) {
        this.center = center;
        this.radius = radius;

        left = center - radius;
        right = center + radius;
    }

}

public class Main {
    public static void main(String[] args)
    {

        FastReader fr = new FastReader();
        int roundCnt = Integer.parseInt(fr.nextLine());

        String[] roundInfo;
        ArrayList<Round> arrList = new ArrayList<>();
        for(int i = 0; i < roundCnt; ++i)
        {
            roundInfo = fr.nextLine().split(" ");

            arrList.add(new Round(Integer.parseInt(roundInfo[0]), Integer.parseInt(roundInfo[1])));
        }

        arrList.sort((r1, r2)-> r1.left-r2.left);

        for(int i = 0; i < arrList.size(); ++i)
        {
            if((i+1 <arrList.size()) &&
               (arrList.get(i).left <= arrList.get(i+1).left) &&
               (arrList.get(i).right >= arrList.get(i+1).left) &&
               (arrList.get(i).right <= arrList.get(i+1).right))
            {
                System.out.println("NO");
                return;
            }
        }

        System.out.println("YES");
    }

    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 nextLine()
        {
            String str = "";

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

            return str;
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글