๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1931 ํšŒ์˜์‹ค ๋ฐฐ์ •

by Jouureee 2021. 2. 17.

๋ฌธ์ œ : 

www.acmicpc.net/problem/1931

 

1931๋ฒˆ: ํšŒ์˜์‹ค ๋ฐฐ์ •

(1,4), (5,7), (8,11), (12,14) ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

 

๋ถ„์„ : 

์ด ๋ฌธ์ œ๋Š” ๋Œ€ํ‘œ์ ์ธ greedy algorithm์œผ๋กœ ํ‘ธ๋Š” ๋ฌธ์ œ์ด๋‹ค. ํšŒ์˜์‹ค, ๊ฐ€๋ฐฉ ๋ฐฐ๋‚ญ ๋“ฑ ๊ณ ์ „์ ์ธ ๋ฌธ์ œ ์œ ํ˜•์ธ๊ฑฐ ๊ฐ™๋‹ค.

๋ฒกํ„ฐ์— ์ธํ’‹๊ฐ’์„ ๋ชจ๋‘ ๋‹ด์€ ๋’ค ํ•ต์‹ฌ์€ ํšŒ์˜๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„ second ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฒ€์‚ฌํ•˜๋Š”๋ฐ ์ด์ „ ํšŒ์˜์˜ ๋๋‚˜๋Š” ์‹œ๊ฐ„ < ๋‹ค์Œ ํšŒ์˜ ์‹œ๊ฐ„ ์ด๋ฉด์€ ํšŒ์˜๊ฐ€ ์‹œ์ž‘ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ cnt ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ ํšŒ์˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ์…Œ๋‹ค. 

 

c++ ์ฝ”๋“œ :

//
//  1931_room.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/10.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;



int assign_meeting_room(int n, vector<pair<int, int>> v){
    int cnt = 1; // ์ œ์ผ ๋นจ๋ฆฌ ๋๋‚˜๋Š” ๊ฑฐ ์ผ๋‹จ ๋„ฃ์–ด ๋†“๊ธฐ
    
    sort(v.begin(), v.end(), [](auto &left, auto &right) { // second ์ˆœ์œผ๋กœ ์ •๋ ฌ
        if(left.second == right.second){
            return left.first < right.first;
        }
        return left.second < right.second;
    });
    
    int before_meet = 0;
    for(int i = 1; i < (int)v.size(); i++){
        if(v.at(i).first >= v.at(before_meet).second){
            cnt++; //put meeting
            before_meet = i;
        }
    }

    
    return cnt;
}


int main(){

    int n;
    cin >> n;
    
    vector<pair<int, int>> v;
    int start, finish;
    for(int i =0; i < n; i++){
        cin >> start >> finish;
        v.push_back(make_pair(start, finish));
        //cout << v[i].front().first << v[i].front().second << endl;
    }
    int count = assign_meeting_room(n, v);
    cout << count << endl;
    return 0;
}

๋Œ“๊ธ€