[백준/BOJ] 19539. 사과나무 [Gold 5]

jychan99·2023년 9월 1일
0

  1. 사과나무
    문제출처 : https://www.acmicpc.net/problem/19539

code

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

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int N,sum=0,temp,cnt=0;
    static boolean result;
    static ArrayList<Integer> list = new ArrayList<>();

    public static void main(String args[]) throws IOException{
        N = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());

        while(st.hasMoreTokens()){
            temp = Integer.parseInt(st.nextToken());
            sum+=temp;
            cnt+=temp/2;
            list.add(temp);
        }

        if(sum%3==0){
            if(cnt>=sum/3){
                result=true;
            }else{
                result=false;
            }
        }else{
            result=false;
        }

        if(result){
            System.out.print("YES");
        }else{
            System.out.print("NO");
        }
    }
}

참고블로그 : https://velog.io/@axiom0510/b19539
수학적으로 풀이를 잘해놓으셔서 이해가 빠르게되었다.

3의배수까진 알겠는데 2짜리 물뿌리개를 최대한 활용해야한다는 생각까지는 못해서 아쉽다.....

profile
내가 지금 두려워 하고 있는 일이 바로 내가 지금 해야 할 일이다. 🐥

0개의 댓글