๋ฌธ์
12764๋ฒ: ์ธ์ง๋ฐฉ์ ๊ฐ ์คํ
์ฒซ์งธ ์ค์ ์ฌ๋์ ์๋ฅผ ๋ํ๋ด๋ \(N\)์ด ์ฃผ์ด์ง๋ค. \((1 \le N \le 100,000)\) ๋์งธ ์ค๋ถํฐ \(N\)๊ฐ์ ์ค์ ๊ฑธ์ณ์ ๊ฐ ์ฌ๋์ ์ปดํจํฐ ์ด์ฉ ์์ ์๊ฐ \(P\)์ ์ข ๋ฃ ์๊ฐ \(Q\)๊ฐ ์ฃผ์ด์ง๋ค. \((0 \le P \lt Q \le 1,000
www.acmicpc.net
์ฝ๋
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int N; cin>>N;
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<>> min_heap; // {๋๋๋ ์๊ฐ, ์ข์๋ฒํธ}
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<>> seats; // {์ข์๋ฒํธ, ๋๋๋ ์๊ฐ}
vector<pair<int,int>>time;
vector<int>cnt(N);
int ans = 0;
for(int i=0; i<N; i++){
int start, end; cin>>start>>end;
time.push_back({start,end});
}
sort(time.begin(),time.end());
int computer_num = 0;
for(int i=0; i<N; i++){
int start = time[i].first;
int end = time[i].second;
while(!min_heap.empty()){
if(min_heap.top().first<start){
seats.push({min_heap.top().second, min_heap.top().first});
min_heap.pop();
}
else break;
}
if(seats.empty()){
min_heap.push({end, ++computer_num});
cnt[computer_num]++;
}
else{
int seat = seats.top().first;
min_heap.push({end, seat});
cnt[seat]++;
seats.pop();
}
}
cout<<computer_num<<'\n';
for(int i=1; i<=computer_num; i++){
cout<<cnt[i]<<' ';
}
}
ํ์ด
11000๋ฒ: ๊ฐ์์ค ๋ฐฐ์
์ฒซ ๋ฒ์งธ ์ค์ N์ด ์ฃผ์ด์ง๋ค. (1 ≤ N ≤ 200,000) ์ดํ N๊ฐ์ ์ค์ Si, Ti๊ฐ ์ฃผ์ด์ง๋ค. (0 ≤ Si < Ti ≤ 109)
www.acmicpc.net
์ฒ์์๋ ํ ํ๋๋ก ์ ๋ฌธ์ ์ ๋น์ทํ๋ค๊ณ ์๊ฐํ์ฌ ํ๋์ ํ์ผ๋ก ๊ตฌํํ์์ง๋ง "์๋ก์ด ์ฌ๋์ ์ด์ ์ฌ๋์ด ์ด์ฉํ๋ ์ข์์ด ์๋๋ผ ๋น์ด์๋ ์ข์ ์ค์์ ๋ฒํธ๊ฐ ๊ฐ์ฅ ์์ ์ข์์ ์์์ผ ํฉ๋๋ค."๋ผ๋ ์กฐ๊ฑด ๋๋ฌธ์ heap์ ๋ ๊ฐ๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํด์ผ ํฉ๋๋ค.
6
0 15
10 25
20 30
50 70
60 80
100 120
ํนํ ์์ ๊ฐ์ ์ ๋ ฅ์ผ ๋๋ฅผ ์๊ฐํ์ฌ ๊ณ ๋ฏผํ๋ฉด ์กฐ๊ธ ๋ ์กฐ๊ฑด์ ๋ถํฉํ๊ฒ ๊ณ ๋ฏผํ ์ ์์ต๋๋ค.
min_heap๊ณผ seats๋ฅผ ๊ฐ๊ฐ ์ต์ ํ์ผ๋ก {๋๋๋ ์๊ฐ, ์ข์๋ฒํธ}, {์ข์๋ฒํธ, ๋๋๋ ์๊ฐ}์ผ๋ก ์ ์ฅํ ๋ค ๋น ์ข์๋ค์ ์ ๋ถ seats๋ก ์ฎ๊ฒจ์ ๋น๊ตํ๋ ์์ผ๋ก ํด๊ฒฐํ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 5539๋ฒ - ์ด์ง ๊ฒ์ ํธ๋ฆฌ (0) | 2022.06.21 |
---|---|
[๋ฐฑ์ค,c++] 9934๋ฒ - ์์ ์ด์ง ํธ๋ฆฌ (0) | 2022.06.21 |
[๋ฐฑ์ค,c++] 1655๋ฒ - ๊ฐ์ด๋ฐ๋ฅผ ๋งํด์ (0) | 2022.06.21 |
[๋ฐฑ์ค,c++] 17255๋ฒ - N์ผ๋ก ๋ง๋ค๊ธฐ (0) | 2022.06.20 |
[๋ฐฑ์ค,c++] 19537๋ฒ - ์ธ์ด๋ฒ๊ฐ๊ฐ์ดํ (0) | 2022.06.20 |
[๋ฐฑ์ค,c++] 10546๋ฒ - ๋ฐฐ๋ถ๋ฅธ ๋ง๋ผํ ๋ (0) | 2022.06.19 |
๋๊ธ