반응형

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

 

9576번: 책 나눠주기

백준이는 방 청소를 하면서 필요 없는 전공 서적을 사람들에게 나눠주려고 한다. 나눠줄 책을 모아보니 총 N권이었다. 책이 너무 많기 때문에 백준이는 책을 구분하기 위해 각각 1부터 N까지의

www.acmicpc.net

 

b 오름차순, a 오름차순으로 정렬한 뒤,

낮은 번호의 책 먼저 할당해주면 최대 학생 수를 구할 수 있습니다.

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
#define MAX 1001

int t, n, m, a, b;
vector<pair<int, int>> arr;
bool visit[MAX] = { false };

bool comp(pair<int, int>& a, pair<int, int>& b) {
	if (a.second == b.second) {
		return a.first < b.first;
	}
	else {
		return a.second < b.second;
	}
}

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

	cin >> t;
	while (t--) {
		arr.clear();
		memset(visit, false, sizeof(visit));

		cin >> n >> m;
		for (int i = 0; i < m; i++) {
			cin >> a >> b;
			arr.push_back({ a, b });
		}

		sort(arr.begin(), arr.end(), comp);

		int cnt = 0;
		for (auto& it : arr) {
			for (int i = it.first; i <= it.second; i++) {
				if (!visit[i]) {
					visit[i] = true;
					cnt++;
					break;
				}
			}
		}
		cout << cnt << "\n";
	}
}
반응형

'Algorithm' 카테고리의 다른 글

백준 2866 : 문자열 잘라내기  (0) 2021.11.11
백준 10546 : 배부른 마라토너  (0) 2021.11.11
백준 1700 : 멀티탭 스케줄링  (0) 2021.11.11
백준 16234 : 인구 이동  (0) 2021.11.11
백준 1647 : 도시 분할 계획  (0) 2021.11.11

+ Recent posts