https://www.acmicpc.net/problem/15831
처음에는 이중 반복문으로 짜서 두 번째 조건에서는 실패했습니다.
효율적으로 해보려 해도 도저히 생각이 나지 않아서 구글링 해보니,
슬라이딩 윈도우라는 기법을 알게 되었습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <iostream> #include <queue> #include <algorithm> using namespace std; int main(){ ios_base::sync_with_stdio(false); queue<char> q; int n,b,w, ret=0; int w_chk = 0, b_chk = 0; cin >> n >> b >> w; for(int i = 0; i < n; i++){ char tmp; cin >> tmp; q.push(tmp); if(tmp == 'B') b_chk+=1; else w_chk +=1; if(b_chk <= b && w_chk >= w) ret = max(ret, int(q.size())); while(b_chk >b){ char tmp = q.front(); q.pop(); if(tmp=='W') w_chk-=1; else b_chk-=1; } } cout << ret; } | cs |
반응형
'프로그래밍 > 문제풀이' 카테고리의 다른 글
[etc] 백준 13118 뉴턴과 사과 (0) | 2019.01.15 |
---|---|
[dp, bfs] 백준 16568 엔비스카의 영혼 (0) | 2019.01.05 |
[etc] 백준 15828 Router (0) | 2018.12.28 |
[etc] 백준 14612 김식당 (0) | 2018.12.28 |
[재귀] 백준 11578 팀원 모집 (0) | 2018.12.27 |