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

 

1379번: 강의실 2

첫째 줄에 강의의 개수 N(1≤N≤100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 줄마다 세 개의 정수가 주어지는데, 순서대로 강의 번호, 강의 시작 시간, 강의 종료 시간을 의미한다. 강의 번

www.acmicpc.net

 

핵심::

n log n 복잡도로 강의실 사용 추적 

 

풀이::

강의실 문제에서 백트래킹이 추가되었다. 방 개수 세는 로직은 같고

 

stl set으로 사용 가능한 방번호를 계속 추적했다.

 

더보기
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>

using namespace std;

struct cl {
	int cn, s, e;
};

struct room {
	int no, use;
};

struct cmps {
	bool operator()(cl& a, cl& b) {
		if (a.e == b.e) {
			return a.s < b.s;
		}
		return a.e > b.e;
	}
};

priority_queue<cl, vector<cl>, cmps> pq;

bool cmp(cl a, cl b) {
	return a.s < b.s;
}

int main()
{
	int n;
	cin >> n;
	vector<int> arr(n + 1);
	vector<cl> clist(n + 1);
	set<int, less<int>> room;

	for (int i = 1; i <= n; i++) {
		int et;
		int s, e;
		scanf("%d %d %d", &et, &s, &e);
		clist[i] = { et, s, e };
		room.insert(i);
	}

	sort(clist.begin(), clist.end(), cmp);

	arr[clist[1].cn] = 1;
	int mx = -1;
	for (int i = 1; i <= n; i++) {
		int rn = -1;
		while (!pq.empty() && pq.top().e <= clist[i].s) {
			rn = arr[pq.top().cn];
			room.insert(rn);
			pq.pop();
		}
		pq.push(clist[i]);
		int sz = pq.size();
		mx = mx > sz ? mx : sz;
		if (rn == -1) {			
			int fr = *room.begin();
			arr[clist[i].cn] = fr;
			room.erase(fr);
		}
		else {
			arr[clist[i].cn] = rn;
			room.erase(rn);
		}
	}

	cout << mx << "\n";
	for (int i = 1; i <= n; i++) {
		printf("%d\n", arr[i]);
	}
	return 0;
}