일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조
- OS
- 감상문
- Computer Science
- 네트워크
- BOJ
- 데이터베이스
- language
- D3
- 문제풀이
- db
- 알고리즘
- network
- data structure
- Programmers
- SW Expert Academy
- algorithm
- c++
- 법의학
- algogritim
- cs
- LeetCode
- 재테크/투자
- 독서
- 운영체제
- D2
- 프로그래머스
- Database
- 백준
- swea
Archives
- Today
- Total
선택은 나의 것
[BOJ 백준] 12764번 싸지방에 간 준하 본문
문제
BOJ number : https://www.acmicpc.net/problem/12764
12764번: 싸지방에 간 준하
첫째 줄에 사람의 수를 나타내는 \(N\)이 주어진다. \((1 \le N \le 100,000)\) 둘째 줄부터 \(N\)개의 줄에 걸쳐서 각 사람의 컴퓨터 이용 시작 시각 \(P\)와 종료 시각 \(Q\)가 주어진다. \((0 \le P \lt Q \le 1,000
www.acmicpc.net
접근
우선순위 큐와 set을 이용하여 자동으로 정렬이 되는 컨테이너라는 특성을 이용하여 문제를 해결할 수 있었다.
코드
#include <iostream>
#include <queue>
#include <set>
#define endl "\n"
using namespace std;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pe, out;
set<int> seat;
int seatCount[100010] = {0};
int main() {
int n, p, q, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p >> q;
pe.push(make_pair(p, q));
}
for (int i = 0; i < n; i++) {
pair<int, int> top = pe.top();
pe.pop();
while (!out.empty() && out.top().first <= top.first) {
seat.insert(out.top().second);
out.pop();
}
if (seat.empty()) {
seatCount[ans]++;
out.push(make_pair(top.second, ans));
ans++;
} else {
seatCount[*seat.begin()]++;
out.push(make_pair(top.second, *seat.begin()));
seat.erase(seat.begin());
}
}
cout << ans << endl;
for (int i = 0; i < ans; i++)
cout << seatCount[i] << " ";
return 0;
}
'☽ Algorithm > BOJ' 카테고리의 다른 글
[BOJ 백준] 6549번 히스토그램에서 가장 큰 직사각 (0) | 2021.07.14 |
---|---|
[BOJ 백준] 1725번 히스토그램 (0) | 2021.07.13 |
[BOJ 백준] 17609번 회문 (0) | 2021.07.07 |
[BOJ 백준] 5582번 공통 부분 문자열 (0) | 2021.07.06 |
[BOJ 백준] 10025번 게으른 백곰 (0) | 2021.07.05 |
Comments