xortl98 2025. 4. 9. 14:40
728x90

 출처 

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

 내 풀이 

더보기
더보기
#include<iostream>
#include<vector>

using namespace std;

int N, K;
int result;
int check;

int main()
{
	cin >> N >> K;
	
	vector<int> temp(N, 0);

	for (int i = 0; i < N; i++)
	{
		cin >> temp[i];

		if (i < K)
		{
			result += temp[i];
			check += temp[i];
		}

		else
		{
			check += temp[i] - temp[i - K];
			if (result <= check)
			{
				result = check;
			}
		}
	}
	cout << result;
}

 

 해설

K가 5였을 때 기준으로 0~4까지 다 더해주고 그 다음부터는 0 빼고 5더하고 -> 1 빼고 6 더하고 하는 식으로 check 업데이트 및 result와 비교하여 최고일 때 정답 처리를 하였음

 

 느낀점 

else 문안에 또 if 안붙이고도 간단하게 result = max(result, check); 해도 된다고 함..

추후에는 그렇게 해서 다시 공부해보기