두 개의 탑 2118

LJM·2023년 2월 27일
0

백준풀기

목록 보기
117/259

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

어떻게 풀어야 할지 감이오지 않아서 고민하다가 결국 풀이를 보고 참고하였다

r 은 rabbit
t 는 turtle 라고 생각하고 짬
rhead 는 r 과 t 사이의 거리
thead 는 t(가 머리가되는) 와 r 사이의 거리

초기값은 rhead = 1
thead = 14

rhead 와 thead 비교해서
rhead 가 더 작으면 r++
thead 가 더 작으면 t++ 해가면서 rhead 와 thead 거리중 작은것을 max와 비교하고 더 큰값을 max에 넣는다

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

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        int[] arr = new int[N];

        for(int i = 0; i < N; ++i)
        {
            arr[i] = Integer.parseInt(br.readLine());
        }

        int rab = 1;
        int tur = 0;
        int rabhead = arr[0];
        int turhead = 0;
        for(int i = 1; i < N; ++i)
        {
            turhead += arr[i];
        }

        int max = Integer.MIN_VALUE;

        while(tur < N)
        {

            if(rabhead <= turhead)
            {
                rabhead += arr[rab];
                turhead -= arr[rab];

                rab++;
                if(rab >= N)
                    rab = 0;
            }
            else
            {
                turhead += arr[tur];
                rabhead -= arr[tur];

                tur++;
                //if(tur >= N)
                //    tur = 0;
            }

            max = Math.max(max, Math.min(rabhead, turhead));
        }

        System.out.println(max);
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글