πŸ“¦ Chango/πŸš™ Softeer

[Softeer][데브크루 2일차] μ†Œν”„ν‹°μ–΄ Lv.2 νšŒμ˜μ‹€ μ˜ˆμ•½ (21λ…„ 재직자 λŒ€νšŒ μ˜ˆμ„ )

선달 2024. 1. 26. 05:13
λ°˜μ‘ν˜•

https://softeer.ai/practice/6266

 

Softeer - ν˜„λŒ€μžλ™μ°¨κ·Έλ£Ή SWμΈμž¬ν™•λ³΄ν”Œλž«νΌ

 

softeer.ai

 

문제 μ„€λͺ…

νšŒμ˜μ‹€μ˜ 수 n μ˜ˆμ•½λœ 회의의 수 m

이후 n개의 쀄에 νšŒμ˜μ‹€μ˜ 이름 r이 주어짐

이어 M개의 쀄에 각 νšŒμ˜κ°€ λ°°μ •λœ νšŒμ˜μ‹€μ˜ 이름 rκ³Ό μ‹œμž‘ μ‹œκ° s, 그리고 μ’…λ£Œ μ‹œκ° tκ°€ 주어짐

3 7
grandeur
avante
sonata
sonata 14 16
grandeur 11 12
avante 15 18
sonata 10 11
avante 9 12
grandeur 16 18
avante 12 15

 

각 νšŒμ˜μ‹€μ— λŒ€ν•œ κ°€λŠ₯ν•œ(νšŒμ˜κ°€ μ—†λŠ”) μ‹œκ°„λŒ€λ₯Ό νšŒμ˜μ‹€ μ΄λ¦„μ˜ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 좜λ ₯ν•˜λ©΄ 됨

Room avante:
Not available
-----
Room grandeur:
2 available:
09-11
12-16
-----
Room sonata:
3 available:
09-10
11-14
16-18

 

 

풀이

μž…μΆœλ ₯이 일단 ꡉμž₯히 λ³΅μž‘ν•˜κ³  κΉκΉν•˜λ‹€.

 

μ‹œκ°„λŒ€λ³„ νšŒμ˜μ‹€μ˜ μƒνƒœλ₯Ό status λ²‘ν„°λ‘œ κ΄€λ¦¬ν–ˆλ‹€

status[i][j] = i번 νšŒμ˜μ‹€μ˜ jμ‹œκ°„μ— κ°€λŠ₯ μ—¬λΆ€

 

이 κ³Όμ •μ—μ„œ νšŒμ˜μ‹€ 이름과 νšŒμ˜μ‹€ 번호λ₯Ό λ§€μΉ­μ‹œν‚€κΈ° μœ„ν•΄ 자료ꡬ쑰 map을 μ‚¬μš©ν–ˆλ‹€

map<νšŒμ˜μ‹€ 이름(ν‚€), νšŒμ˜μ‹€ 번호(κ°’)> status

μ΄λ•Œ νšŒμ˜μ‹€ 이름 영문 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ ν•΄μ•Όν•˜λŠ” 좜λ ₯ 쑰건을 μžλ™μœΌλ‘œ ν•  수 μžˆλ‹€λŠ”κ±΄ 덀

(맡 μžλ£Œκ΅¬μ‘°λŠ” ν‚€λ₯Ό μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ €μž₯ν•œλ‹€)

    // νšŒμ˜μ‹€μ— 번호 λΆ€μ—¬
    map<string, int> room;
    string r;
    for(int i=0; i<n; i++) {
        cin >> r;
        room.insert({r, i});
    }

 

μ½”λ“œ

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int,int> ci;

// 9μ‹œλΆ€ν„° 18μ‹œκΉŒμ§€ μ‹œκ°„λŒ€ 갯수
const int TIME = 9; 
const int START = 9;
const int END = 18;

int main() {
    int n, m;
    cin >> n >> m;

    // νšŒμ˜μ‹€μ— 번호 λΆ€μ—¬
    map<string, int> room;
    string r;
    for(int i=0; i<n; i++) {
        cin >> r;
        room.insert({r, i});
    }

    // 각 νšŒμ˜μ‹€ μ˜ˆμ•½λ‚΄μ—­ μΆ”κ°€
    vector<vector<ci>> reserve(n, vector<ci>());
    int s, t;
    for(int i=0; i<m; i++) {
        cin >> r >> s >> t;
        int room_num = room[r];
        reserve[room_num].push_back({s,t});
    }

    int cnt = 0;
    for(auto iter=room.begin(); iter!=room.end(); iter++) {
        // κ°€λŠ₯ν•œ μ‹œκ°„λŒ€ κ΅¬ν•˜κΈ°
        int room_num = room[iter->first];
        vector<ci> unavailable = reserve[room_num];
        sort(unavailable.begin(), unavailable.end());
        vector<ci> available;
        
        int cur = START;
        for(ci i : unavailable) {
            if(i.first != cur) {
                available.push_back({cur, i.first});
            }
            cur = i.second;
        }
        if(cur != END) {
            available.push_back({cur, END});
        }

        // 좜λ ₯ν•˜κΈ°
        cout << "Room " << iter->first << ":\n";
        if(available.size() > 0) {
            cout << available.size() << " available:\n";
            for(ci i : available) {
                if(i.first<10) cout << "0";
                cout << i.first << "-" << i.second << "\n";
            }
        } else {
            cout << "Not available\n";
        }
    
        cnt++;
        if(cnt != n) {
            cout << "-----\n";
        }
    }

    return 0;
}

 

 

μ‹œν–‰μ°©μ˜€

더보기

이제 m개 쀄을 돌며 νšŒμ˜μ‹€ μ˜ˆμ•½μ„ ν‘œμ‹œν•œλ‹€

avante 15 18 이 μ£Όμ–΄μ‘Œλ‹€λ©΄ 

map[avante]λ₯Ό 톡해 avanteνšŒμ˜μ‹€μ˜ 번호 0을 뽑아내고

status[0][6] λΆ€ν„° status[0][9] κΉŒμ§€λ₯Ό false 둜 μ±„μš°λŠ” ν˜•μ‹

15λΆ€ν„° 18이 μ•„λ‹Œ 6λΆ€ν„° 9인 μ΄μœ λŠ” νšŒμ˜μ‹€ κ°€λŠ₯ 졜초 μ‹œκ°„μΈ 9λ₯Ό 각각 빼쀬음

    // νšŒμ˜μ‹€ μƒνƒœ μ—…λ°μ΄νŠΈ
    vector<vector<bool>> status(n, vector<bool>(TIME, true));
    int s, t;
    for(int i=0; i<m; i++) {
        cin >> r >> s >> t;
        int room_num = room[r];
        for(int i=s; i<t; i++) {
            status[room_num][i-START] = false;
        }
    }

 

 

이제 κ°€λŠ₯ν•œ μ‹œκ°„λŒ€λ₯Ό κ΅¬ν•΄μ•Όν•œλ‹€.

status 맡을 μˆœνšŒν•˜λ©΄μ„œ κ°€λŠ₯ν•œ μ‹œκ°„λŒ€λ₯Ό κ΅¬ν•˜κ³  λ°”λ‘œ 좜λ ₯ν•΄μ£Όμž.

 

μ‹œκ°„λŒ€λ³„ κ°€λŠ₯ μ—¬λΆ€κ°€ λ²‘ν„°λ‘œ μ£Όμ–΄μ‘Œμ„ λ•Œ κ°€λŠ₯ν•œ μ‹œκ°„λŒ€λŠ” μ–΄λ–»κ²Œ κ΅¬ν• κΉŒ?

 

각 νšŒμ˜μ‹€μ— ν•΄λ‹Ήν•˜λŠ” status의 μ‹œκ°„λŒ€λ³„ κ°€λŠ₯ μ—¬λΆ€λ₯Ό ν™•μΈν•˜λ©΄μ„œ

(1)κ°€λŠ₯ν–ˆλ‹€κ°€ λΆˆκ°€λŠ₯ν•΄μ§€λŠ” μ‹œμ 

(2)λΆˆκ°€λŠ₯ν–ˆλ‹€κ°€ κ°€λŠ₯ν•΄μ§€λŠ” μ‹œμ 

이 두가지λ₯Ό branch 벑터에 μ €μž₯ν•œλ‹€.

// κ°€λŠ₯ν•œ μ‹œκ°„λŒ€ κ΅¬ν•˜κΈ°
    int cnt = 0;
    for(auto iter=room.begin(); iter!=room.end(); iter++) {
        int room_num = room[iter->first];
        
        vector<int> branch; // κ°€λŠ₯κ³Ό λΆˆκ°€λŠ₯ 경계 μ‹œκ°„λŒ€ λ„£κΈ°
        bool tmp = true;
        for(int j=0; j<TIME; j++) {
            if(status[room_num][j] == tmp) {
                tmp = !tmp;
                branch.push_back(j+START);
            }
        }

 

status 벑터가 λ‹€μŒκ³Ό κ°™λ‹€κ³  κ°€μ •ν•œλ‹€λ©΄,

μ‹œκ°„λŒ€ 1 2 3 4 5 6 7
κ°€λŠ₯μ—¬λΆ€ o x x o o x o

 

μ˜ˆμ‹œμ—μ„œλŠ” 1 2 4 6 7 이 μ €μž₯될거닀

 

그럼 이λ₯Ό ν•΄μ„ν•˜λ©΄ κ²°κ΅­ κ°€λŠ₯ν•œ μ‹œκ°„λŒ€κ°€

1-2, 4-6, 7-λκΉŒμ§€

μ΄λ ‡κ²Œ μ„Έκ°€μ§€λΌλŠ” κ±Έ μ•Œ 수 μžˆλ‹€

 

즉, branch 벑터에 μžˆλŠ” 값을 μ°¨λ‘€λŒ€λ‘œ 좜λ ₯만 ν•΄μ£Όλ©΄ λœλ‹€!

branch의 크기 / 2 λŠ” κ°€λŠ₯ν•œ μ‹œκ°„λŒ€μ˜ μˆ˜μ™€ κ°™κΈ° λ•Œλ¬Έμ—

Available: branch.size()/2 둜 κ°€λŠ₯ν•˜λ‹€.

 

참고둜 좜λ ₯ 쑰건에 μžˆλŠ” "-----" μ΄κ±°λŠ” 맨 λ§ˆμ§€λ§‰ λ°© λ°‘μ—λŠ” μ—†λ‹€λŠ” 점 μ£Όμ˜ν•˜μž

(이걸 μœ„ν•΄ λ§ˆμ§€λ§‰ 방인 경우 -----λ₯Ό 좜λ ₯ μ•ˆν•˜λ„λ‘ cnt λ³€μˆ˜λ₯Ό λ„£μ—ˆλ‹€)

// 좜λ ₯
        cout << "Room " << iter->first <<":\n";
        if(branch.size() == 0) {
            cout << "Not available\n";
        } else {
            cout << branch.size()/2 + branch.size()%2 << " available:\n";
            for(int j=0; j<branch.size(); j+=2) {
                cout << timeString(branch[j]) << "-" << timeString(branch[j+1]) << "\n";
            }
        }

        cnt++;
        if(cnt != n) {
            cout << "-----\n";
        }

 

 

참고둜 좜λ ₯ν•  λ•Œ 숫자 9λŠ” 09둜 ν‘œμ‹œν•΄μ•Όν•˜κ³ 

λ§Œμ•½ λ§ˆμ§€λ§‰ μ‹œκ°„κΉŒμ§€ μ‚¬μš© κ°€λŠ₯ν•΄μ„œ branch κ°―μˆ˜κ°€ ν™€μˆ˜μΈ 경우

λ§ˆμ§€λ§‰ μ€„μ˜ λ‘λ²ˆμ§Έ 수(branch[j+1])κ°€ 접근이 μ•ˆλ˜μ–΄μ„œ 0이 λ‚˜μ˜¨λ‹€.

이 μ˜ˆμ™Έ μΌ€μ΄μŠ€λ₯Ό μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ timeString ν•¨μˆ˜λ₯Ό μž„μ˜λ‘œ λ§Œλ“€μ–΄μ„œ 상단에 μ •μ˜ν–ˆλ‹€

// 9μ‹œλΆ€ν„° 18μ‹œκΉŒμ§€ μ‹œκ°„λŒ€ 갯수
const int TIME = 9; 
const int START = 9;
const int END = 18;

string timeString(int a) {
    if(a==0) return to_string(END);
    if(a<10) return "0"+ to_string(a);
    return to_string(a);
}

 

 

μ½”λ“œ

#include <iostream>
#include <vector>
#include <map>

using namespace std;

// 9μ‹œλΆ€ν„° 18μ‹œκΉŒμ§€ μ‹œκ°„λŒ€ 갯수
const int TIME = 9; 
const int START = 9;
const int END = 18;

string timeString(int a) {
    if(a==0) return to_string(END);
    if(a<10) return "0"+ to_string(a);
    return to_string(a);
}

int main() {
    int n, m;
    cin >> n >> m;

    // νšŒμ˜μ‹€μ— 번호 λΆ€μ—¬
    map<string, int> room;
    string r;
    for(int i=0; i<n; i++) {
        cin >> r;
        room.insert({r, i});
    }

    // νšŒμ˜μ‹€ μƒνƒœ μ—…λ°μ΄νŠΈ
    vector<vector<bool>> status(n, vector<bool>(TIME, true));
    int s, t;
    for(int i=0; i<m; i++) {
        cin >> r >> s >> t;
        int room_num = room[r];
        for(int i=s; i<t; i++) {
            status[room_num][i-START] = false;
        }
    }

    // κ°€λŠ₯ν•œ μ‹œκ°„λŒ€ κ΅¬ν•˜κΈ°
    int cnt = 0;
    for(auto iter=room.begin(); iter!=room.end(); iter++) {
        int room_num = room[iter->first];
        
        vector<int> branch; // κ°€λŠ₯κ³Ό λΆˆκ°€λŠ₯ 경계 μ‹œκ°„λŒ€ λ„£κΈ°
        bool tmp = true;
        for(int j=0; j<TIME; j++) {
            if(status[room_num][j] == tmp) {
                tmp = !tmp;
                branch.push_back(j+START);
            }
        }

        // 좜λ ₯
        cout << "Room " << iter->first <<":\n";
        if(branch.size() == 0) {
            cout << "Not available\n";
        } else {
            cout << branch.size()/2 + branch.size()%2 << " available:\n";
            for(int j=0; j<branch.size(); j+=2) {
                cout << timeString(branch[j]) << "-" << timeString(branch[j+1]) << "\n";
            }
        }

        cnt++;
        if(cnt != n) {
            cout << "-----\n";
        }
    }

    return 0;
}

 

λ°˜μ‘ν˜•