문제접근
•
모든 경우를 확인해야하기 때문에 앞에 하나 빼고 뒤에 하나 더하고 식을 진행
놓쳤던 부분
•
하나 빼고 하나 더하고 과정의 범위 설정 실패
•
tmp = max_value - input[i] + input[i + x]의 식으로 코딩하여 max_value가 업데이트가 항상 되는 것이 아니기 때문에 슬라이딩 방식이 적용되지 않았음
코드
3976 KB
24 ms
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
int n, x;
int count = 1;
int max_value = 0;
vector<long long> input;
int tmp;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> x;
input.resize(n);
for (int i = 0; i < n; i++)
cin >> input[i];
for (int i = 0; i < x; i++)
max_value += input[i];
tmp = max_value;
for (int i = 0; i <= n - x - 1; i++)
{
tmp = tmp - input[i] + input[i + x];
if (tmp > max_value)
{
count = 1;
max_value = tmp;
}
else if (tmp == max_value)
count++;
}
if (max_value == 0)
cout << "SAD";
else
cout << max_value << "\n" << count;
return (0);
}
C++
복사