๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 12764๋ฒˆ - ์‹ธ์ง€๋ฐฉ์— ๊ฐ„ ์ค€ํ•˜

by ์•ˆ์ฃผํ˜• 2022. 6. 20.

๋ฌธ์ œ

 

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๋กœ ์˜ฎ๊ฒจ์„œ ๋น„๊ตํ•˜๋Š” ์‹์œผ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€