Study/Algorithm

백준 21921번

_WooHyun_ 2022. 2. 21. 03:21

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

 

21921번: 블로그

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

www.acmicpc.net

#include <iostream>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int N, X;
	cin >> N >> X;

	int arr[250001];
	for (int i = 0; i < N; i++)
		cin >> arr[i];

	int start = 0, end = start + X - 1;
	int max = 0, answer =0;
	while (true) {
		int sum = 0;
		start++;
		end++;
		for (int i = start; i <= end; i++) {
			sum += arr[i];
		}
		
		if (max < sum) {
			max = sum;
			answer = 1;
		}
		else if(max == sum) {
			answer++;
		}

		if (start <= N - X)
			break;
	}

	if (max == 0) {
		cout << "SAD";
	}
	else {
		cout << max << "\n" << answer;
	}
}