๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/PS

[๋ฐฑ์ค€] ์ˆ˜์—ด(2559), C++

by candosh 2024. 11. 13.

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

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

โœ๐Ÿป ๋ฌธ์ œ ์„ค๋ช…

 

๐Ÿ’๐Ÿป‍โ™€๏ธ ๋‚ด ํ’€์ด

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋ˆ„์ ํ•ฉ์ด๋‹ค.

 

psum[i] = psum[i-1] + nextVal;

 

๋ˆ„์ ํ•ฉ์„ ํ’€ ๋• ์œ„์™€ ๊ฐ™์€ ๋Š๋‚Œ์„ ๊ธฐ์–ตํ•˜์ž!

 

๐Ÿค– ์ฝ”๋“œ

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, k, psum[100001] = {0,},
			  maxTmp = -10000004, tmp;

	cin >> n >> k;

	for (int i = 1; i <= n; i++)
	{
		cin >> tmp;
		psum[i] = psum[i - 1] + tmp;
	}

	for (int i = k; i <= n; i++)
	{
		maxTmp = max(maxTmp, psum[i] - psum[i - k]);
	}

	cout << maxTmp << "\n";

	return 0;
}

 

maxTmp = -10000004;

 

๋ณ€์ˆ˜๋ช…์ด maxTmp์ธ๋ฐ, ์ดˆ๊ธฐํ™” ๊ฐ’์ด '-10000004'์ธ ์ด์œ ๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ๊ฐ€ ์ตœ๋Œ“๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด์„œ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด ์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.