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

11000번: 강의실 배정

첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)

www.acmicpc.net

그리드 알고리즘 어렵네요. 골드 들어가니 진짜 어렵네요. for문 하나에 해결하는건 여전하구 이중 for문 쓰면 바로 시간초과 ㅎㅎ ㅜ . 그리디는  패턴을 잘보면 금방 풀고 못보면 하루 이상 걸리네요 ㅠㅠ 
이번에 키 포인트는 우선순위큐를 잘 이용해서 정렬한 데이터를 찾는게 포인트네요! 

#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;

struct Point {
	int s, e, t, cnt, power;
	char c;
};

void Prints(vector<vector<int>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<bool>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<int>line, int n) {
	for (int i = 0; i < n; i++) {
		cout << line[i] << ",";
	}
	cout << endl;
}
void Prints(string str, int n) {
	for (int i = 0; i < n; i++) {
		cout << str[i] << ",";
	}
	cout << endl;
}

bool checkmaps(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}
bool findstart(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m, int) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}

struct Data {
	long long s;
	long long t;

};
struct GDatas {
	int change;
	int index;
	
	string times;

};
bool cmp(const Data &a,const Data &b) {
	if (a.s == b.s) {
		return a.t < b.t;
	}
	
	return a.s < b.s;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	long long n;
	cin >> n;
	vector<Data>lists;
	for (long long i = 0; i < n; i++) {
		Data d;
		int s;
		int t;
		cin >> s >> t;
		d.s = s;
		d.t = t;
		lists.push_back(d);
	}
	sort(lists.begin(), lists.end(), cmp);

	priority_queue<int,vector<int>,greater<> > pq;
	for (long long j = 0; j < lists.size(); j++) {
	
		if (pq.empty()) {
			pq.push(lists[j].t);
		}
		else {
			if (pq.top() <= lists[j].s) {
					pq.pop();
					pq.push(lists[j].t);
			}
			else {
					pq.push(lists[j].t);
			}
		}
		
	}

	cout << pq.size();
	return 0;
}

'알고리즘 공부' 카테고리의 다른 글

백준 N과M (2)  (0) 2023.08.13
백준 치킨배달  (0) 2023.08.10
백준 색칠하기  (0) 2023.08.06
백준 점프점프  (0) 2023.08.06
백준 신입 사원  (0) 2023.08.06

+ Recent posts