중요 지식


특별한 지식은 필요없다

코드


#include <iostream>
#include <queue>

using namespace std;


int main(){
    int N, K;
    int count=0; 
    queue<int> hamburger;
    queue<int> person;

    cin >> N >> K;
    cin.get();
    
    for(int i = 1; i <= N; i++){
        char ch;
        ch = cin.get();

        // 햄버거, 사람 구분
        if(ch == 'H'){
            if(person.empty()) 
                hamburger.push(i);
            else{
                person.pop();
                count++;
            }
        }else{
            if(hamburger.empty())
                person.push(i);
            else{
                hamburger.pop();
                count++;
            }
        }

        if(!hamburger.empty() && i - hamburger.front() >= K)
            hamburger.pop();

        if(!person.empty() && i - person.front() >= K)
            person.pop();
    }
    
    cout << count;
  
    return 0;
}

풀이


1. 문자를 하나하나 검사해서 햄버거 큐와 사람 큐에 나누는데 들어갈 값은 순번이다

2. 햄버거 큐와 사람 큐는 서로 값의 존재를 확인하고 둘 다 존재한다면 맨 앞의 값을 삭제한다

3. 햄버거 큐와 사람 큐의 front()가 현재 순번과의 거리가 K값보다 크거나 같다면 삭제한다  

  

+ Recent posts