๋ฌธ์ :
๋ถ์ :
์ด ๋ฌธ์ ๋ ์ธ๋ป ๋ณด๋ฉด 19598๋ฒ ์ต์ ํ์์ค ๊ฐ์ ๋ฌธ์ ์ ๋น์ทํด ๋ณด์ธ๋ค.
ํ์ง๋ง N์ ๋ฒ์๊ฐ 200,000d์ผ๋ก O(N^2)์ผ๋ก ํ๋ฉด ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค ๐ข
๊ทธ๋์ ์๋์ผ๋ก ๊ฐ์ ๋๋๋ ์๊ฐ์ด ์งง์ ์น๊ตฌ ์์ผ๋ก ๊ฒ์ํ ์ ์๊ฒ๋ ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ์๋ค. (์ฒ์ ์จ๋ณธ๋ค .. ํํ)
์ฐ์ ์์ํ๋
์๋ฃํ T, Container, ์ฌ์ฉ์ ์ ์ ๋น๊ต ์ฐ์ฐ(struct, class - operator ์ฐ์ฐ์) ์ด 3๊ฐ์ง๋ฅผ ์ ์ํด ์ค์ผ ํ๊ณ
pq.pop์ ์ฌ์ฉ์๊ฐ ์ ์ํ ์ฐ์ ์์ ์์ผ๋ก ๋จผ์ ๋๊ฐ๊ฒ ๋๋ค.
๊ตฌ์ํ ์๊ณ ๋ฆฌ์ฆ
1. ์ฐ์ v ๋ฒกํฐ์ pair ์๋ฃํ์ผ๋ก start, end ๋ฅผ ๋ฃ์ด ๋ฐฐ์ด ์์ฑ
2. v ๋ฒกํฐ ์์ ์๊ฐ ๋น ๋ฅธ ์์ผ๋ก sort
3. ์ฐ์ ์์ ํ pq์ v ๋ฒกํฐ ์ฒซ๋ฒ์งธ ์์ ์ฝ์
4. v๋ฒกํฐ 1๋ฒ์งธ ์ธ๋ฑ์ค๋ถํฐ ๋น๊ต
4-1) ์ฐ์ ์์ ํ pq์ ์๋ ๋๋๋ ์๊ฐ์ด ๊ฐ์ฅ ๋น ๋ฅธ ํ์๊ฐ v ๋ฒกํฐ ์์ ์๊ฐ๋ณด๋ค ํฌ๋ค๋ฉด
๋ค๋ฅธ ๊ฐ์์ค์ ํ ๋นํด์ค์ผ ํ๋ค. ๋ฐ๋ผ์ cnt ์ฆ๊ฐ, ์ฐ์ ์์ ํ์ push
4-2) ์ฐ์ ์์ ํ pq์ ๋๋๋ ์๊ฐ์ด ๋ค์ ํ์ ์์ ์๊ฐ๋ณด๋ค ์๋ค๋ฉด ํ์ ์ด์ด์ ํ ์ ์์ผ๋ฏ๋ก
cnt ์ฆ๊ฐํ์ง ์๊ณ , ์ฐ์ ์์ ํ์ push
c++ ์ฝ๋ :
//
// 11000_lecture_room.cpp
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2021/03/07.
//
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
vector<pair<int, int>> v;
bool startTime(const pair<int, int> &a, const pair<int, int> &b){
if(a.first == b.first){
return a.second < b.second;
}
return a.first < b.first;
}
struct compare {
bool operator()(const pair<int, int> &a, const pair<int, int> &b){
if(a.second == b.second){
//๋๋๋ ์๊ฐ์ด ๊ฐ์ผ๋ฉด ์์ ์๊ฐ์ด ๋น ๋ฅธ ์
return a.first > b.first;
}
//๋๋๋ ์๊ฐ์ด ๋น ๋ฅธ ์
return a.second > b.second;
}
};
priority_queue<pair<int, int>, vector<pair<int, int>>, compare> pq;
int find_room_count(int n){
int cnt = 1;
sort(v.begin(), v.end(), startTime);
//์ ๋ ฌ๋ ์ฒซ๋ฒ์งธ ์์ ์ฝ์
(์์ ์๊ฐ ๋น ๋ฅธ ์)
pq.push(v.at(0));
for(int i = 1; i < n; i++){
if(pq.top().second > v.at(i).first){
pq.push(v.at(i));
cnt++;
}else{
pq.pop();
pq.push(v.at(i));
}
}
return cnt;
}
int main(){
int n;
scanf("%d", &n);
for(int i =0; i < n; i++){
int start, end;
scanf("%d %d", &start, &end);
v.push_back(make_pair(start, end));
}
int count = find_room_count(n);
printf("%d", count);
return 0;
}
'Algorithm๐ฐ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1753 ์ต๋จ ๊ฒฝ๋ก dijkstra (0) | 2021.03.10 |
---|---|
[๋ฐฑ์ค] 12865 ํ๋ฒํ ๋ฐฐ๋ญ knapsack ์๊ณ ๋ฆฌ์ฆ (0) | 2021.03.09 |
[๋ฐฑ์ค] 10974 ๋ชจ๋ ์์ด ๋ฐฑํธ๋ํน (0) | 2021.02.27 |
[๋ฐฑ์ค] 2617 ๊ตฌ์ฌ ์ฐพ๊ธฐ DFS (0) | 2021.02.26 |
[๋ฐฑ์ค] 2003 ์๋ค์ ํฉ2 ํฌ ํฌ์ธํฐ (2) | 2021.02.26 |
๋๊ธ