1일1알

백준 19640번 화장실의 규칙 C++ 본문

알고리즘

백준 19640번 화장실의 규칙 C++

영춘권의달인 2022. 12. 24. 11:00

https://www.acmicpc.net/problem/19640

 

19640번: 화장실의 규칙

위와 같이 줄을 선 경우를 생각해보자. (x, y) 는 사원의 근무 일수가 x, 화장실이 급한 정도가 y임을 나타낸다. [x, y]는 해당 사원이 데카임을 의미한다. 즉, 위의 그림에서 데카는 3번 사원이다.

www.acmicpc.net

 

각 줄을 큐로, 줄의 맨 앞 사람들을 우선순위 큐로 관리해서 문제를 해결하였다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

struct Info {
    int d;
    int h;
    int line;
    bool deka;
    bool operator<(const Info& other) const {
        if (d != other.d) return d < other.d;
        if (h != other.h) return h < other.h;
        return line > other.line;
    }
};

int n, m, k;

vector<queue<Info>> v;
priority_queue<Info> pq;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> m >> k;
    v = vector<queue<Info>>(m);
    for (int i = 0; i < n; i++) {
        int line = i % m;
        int d, h;
        bool deka = i == k;
        cin >> d >> h;
        Info info{ d,h,line,deka };
        v[line].push(info);
    }
    for (auto a : v) {
        if (a.empty()) continue;
        pq.push(a.front());
    }
    int ans = 0;
    while (true) {
        auto curr = pq.top();
        if (curr.deka) break;
        pq.pop();
        ans++;
        int line = curr.line;
        v[line].pop();
        if (v[line].empty() == false) {
            pq.push(v[line].front());
        }
    }
    cout << ans;
}