Official
C - 会議室の予約管理 / Meeting Reservation Management Editorial
by
C - 会議室の予約管理 / Meeting Reservation Management Editorial
by
MMNMM
この問題は、イベントソートを使って解くことができます。
次の \(2\) 種類の出来事を考えます。
- 時刻 \(t\) に会議が開始する。
- 時刻 \(t\) に会議が終了する。
これらを時刻 \(t\) の昇順に並べ、現在いくつの会議が行われているかを管理しながら出来事を処理すればよいです。 会議が行われる時間は右半開区間となっているので、同じ時刻の会議開始と会議終了では、会議終了のほうを先に処理する必要があることに注意してください。
実装例は以下のようになります。
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>
using namespace std;
int main() {
int N, T;
cin >> N >> T;
// 出来事の (時間, 開始か終了か) の組
vector<pair<int, int>> event;
for (int i = 0; i < N; ++i) {
int S, E;
cin >> S >> E;
event.emplace_back(S, 1);
event.emplace_back(E, -1);
}
// 時間でソート
ranges::sort(event);
// 現在行われている会議の数と同時に行われた会議の数の最大値
int now_reservation = 0, max_reservation = 0;
for (int type : event | views::values) { // 時間の昇順に種類を見て
now_reservation += type; // 会議の数を更新
max_reservation = max(max_reservation, now_reservation);
}
// 最大値を出力
cout << max_reservation << endl;
return 0;
}
N, K = map(int, input().split())
# 出来事の (時間, 開始か終了か) の組
event = []
for i in range(N):
S, E = map(int, input().split())
event.append((S, 1))
event.append((E, -1))
# 時間でソート
event.sort()
# 現在行われている会議の数と同時に行われた会議の数の最大値
now_reservation = 0
max_reservation = 0
for _, type in event: # 時間の昇順に種類を見て
now_reservation += type # 会議の数を更新
max_reservation = max(max_reservation, now_reservation)
# 最大値を出力
print(max_reservation)
posted:
last update:
