[백준] 3020 개똥벌레 java

Bong2·2024년 7월 5일
0

알고리즘

목록 보기
43/63

문제 - 3020

문제 풀이


그림과 같을 때 N의 길이를 지날 때 그전에 배열의 값과 현재의 배열의 값을 더해주면 된다.
종유석은 반대로 자라기 때문에 전체의 크기에서 빼주어서 값을 구해준다.

소스코드

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int h = sc.nextInt();

        int up[] = new int[h+1];
        int down[] = new int[h+1];

        //종유석 석순 저장 h 보다 작은 수
        for(int i=0;i<n;i++)
        {
            if(i % 2 ==0) down[sc.nextInt()]++;
            else up[sc.nextInt()]++;
        }

        //누적합
        for(int i=h-1;i>-1;i--)
        {
            up[i] += up[i+1];
            down[i] += down[i+1];
        }

        int ans = n;
        int cnt = 0;

        for(int i=1;i<h+1;i++)
        {
            int total = up[h-i+1] + down[i![](https://velog.velcdn.com/images/pkjeogus/post/79d30983-7630-4351-9107-d323fe829166/image.HEIC)
];
            if( total < ans)
            {
                ans = total;
                cnt = 1;
            }else if(total == ans)
            {
                cnt++;
            }
        }
        System.out.println(ans +" "+cnt);
    }

}

참고사이트

profile
자바 백엔드 개발자로 성장하자

0개의 댓글