Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_21921] 블로그 본문

Study/Problem Solving

[BOJ_21921] 블로그

monitor 2023. 11. 8. 06:51

설명


투 포인터를 활용하였다. 지금 조건이 맞으면 해당 건을 넣고 초기화 돌리는 식으로 진행함

 

코드


package two_pointer;

import java.util.Scanner;

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

        int N = sc.nextInt();
        int X = sc.nextInt();

        int [] arr = new int[N];

        for(int i = 0; i < N; i++){
            arr[i] = sc.nextInt();
        }

        int left = 0;
        int right = X - 1;
        int sum = 0;
        int count = 1;
        int max = 0;
        for(int i = 0; i <= right; i++){
            sum += arr[i];
        }
        max = sum;

        while(right < N - 1){

            sum -= arr[left];
            sum += arr[right + 1];

            if(max < sum){
                max = sum;
                count = 1;
            }
            else if (max == sum){
                count++;
            }
            left++;
            right++;
        }
        if(max == 0){
            System.out.println("SAD");
        }else {
            System.out.println(max + " \n" + count);
        }
    }
}
 

21921번: 블로그

첫째 줄에 $X$일 동안 가장 많이 들어온 방문자 수를 출력한다. 만약 최대 방문자 수가 0명이라면 SAD를 출력한다. 만약 최대 방문자 수가 0명이 아닌 경우 둘째 줄에 기간이 몇 개 있는지 출력한다

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_2470] 두 용액  (1) 2023.11.12
[BOJ_20922] 겹치는 건 싫어  (1) 2023.11.11
[BOJ_11728] 배열 합치기  (0) 2023.11.07
[BOJ_7662] 이중 우선순위 큐  (2) 2023.11.07
[BOJ_2075] N번째로 큰 수  (0) 2023.11.05