반응형
https://www.acmicpc.net/problem/32867
#문제 간단 정리
약간 시뮬레이션 같은 구현
#문제 해결 방법
우선 문제에서 나온데로
순서대로 건반을 쳐야되기때문에
우측값과 좌측값을 따로 관리해서
K 가 넘으면
카운트를 해주는 식으로
그리디한 방식으로 구현해주면 된다
#전체 코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <tuple>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> whites(n);
for (int i = 0; i < n; i++) {
cin >> whites[i];
}
int ans = 0;
int r = whites[0] ,l = whites[0];
for (int i = 1; i < whites.size(); i++) {
if (r < whites[i]) {
r = whites[i];
}
else if (l > whites[i]) {
l = whites[i];
}
if ((r-l)>= k) {
ans++;
r = l = whites[i];
}
}
cout << ans << '\n';
return 0;
}
반응형
'[백준] > C++' 카테고리의 다른 글
백준 12786 INHA SUIT [C++] (0) | 2025.03.06 |
---|---|
백준 14575번 뒤풀이 [C++] (0) | 2025.03.02 |
백준 20291번 파일 정리 [C++] (0) | 2025.02.25 |
백준 24041번 성싶당 밀키트 [C++] (0) | 2025.02.19 |
백준 10975번 데크 소트2 [C++] (0) | 2025.02.19 |