본문 바로가기
프로그래밍/문제풀이

[슬라이딩 윈도우] 백준 15831 준표의 조약돌

by 하용권 2018. 12. 31.

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


반응형