개발자 톡
연습문제 톡
[21년 재직자 대회 예선] 회의실 예약
c++로 풀었습니다.
- 등록일
- 2024-09-27 12:16:52
- 조회수
- 108
- 작성자
- hyymi1469
#include<iostream> #include<set> #include<unordered_map> #include<list> #include<string> using namespace std; #define START_HOUR 9 #define END_HOUR 18 void InsertResultMap( const std::string& meetingRoomName, std::unordered_map<std::string, std::list< std::pair<int, int> > >& resultMap, const int startTime, const int timeIter) { auto resultIter = resultMap.find(meetingRoomName); if(resultIter == resultMap.end()) { std::list<std::pair<int, int>> resultList; resultList.emplace_back(std::pair<int, int>(startTime, timeIter)); resultMap.emplace(meetingRoomName, resultList); } else { auto& resultTimeList = resultIter->second; resultTimeList.emplace_back(std::pair<int, int>(startTime, timeIter)); } } int main(int argc, char** argv) { int m,n; cin >> n >> m; std::set<std::string> meetingRoomSet; for(int i = 0; i < n; ++i) { std::string name = ""; cin >> name; meetingRoomSet.emplace(name); } std::unordered_map<std::string, std::multiset<int>> meetingMap; for(int i = 0; i < m; ++i) { std::string name = ""; int startHour = 0; int endHour = 0; cin >> name >> startHour >> endHour; auto meetingIter = meetingMap.find(name); if(meetingIter == meetingMap.end()) { std::multiset<int> timeSet; timeSet.emplace(startHour); timeSet.emplace(endHour); meetingMap.emplace(name, timeSet); } else { meetingIter->second.emplace(startHour); meetingIter->second.emplace(endHour); } } //////// 로직 시작 /////////// for(auto meetingRoomIter : meetingRoomSet) { auto iter = meetingMap.find(meetingRoomIter); if(iter == meetingMap.end()) { std::multiset<int> timeSet; timeSet.emplace(0); meetingMap.emplace(meetingRoomIter, timeSet); } } std::unordered_map<std::string, std::list< std::pair<int, int> > > resultMap; for(const auto& meetingRoomIter : meetingRoomSet) { const std::string& meetingRoomName = meetingRoomIter; auto meetingIter = meetingMap.find(meetingRoomName); if(meetingIter == meetingMap.end()) continue; int count = 0; int startTime = START_HOUR; int endTime = END_HOUR; for(const auto& timeIter : meetingIter->second) { ++count; bool isStartTime = count % 2; if(isStartTime) // 시작시간 { if(timeIter == 0) break; int gapTime = timeIter - startTime; if(gapTime != 0) { InsertResultMap(meetingRoomName, resultMap, startTime, timeIter); } } else // 종료시간 { startTime = timeIter; } } // 종료시간 처리 int gapTime = endTime - startTime; if(gapTime != 0) { InsertResultMap(meetingRoomName, resultMap, startTime, endTime); } } for (auto iter = meetingRoomSet.begin(); iter != meetingRoomSet.end(); ++iter) { cout << "Room " << *iter << ":" << endl; const auto& resultIter = resultMap.find(*iter); if (resultIter == resultMap.end()) { cout << "Not available" << endl; if (std::next(iter) != meetingRoomSet.end()) cout << "-----" << endl; continue; } auto& resultInfoIter = resultIter->second; cout << resultInfoIter.size() << " available:" << endl; for (auto iter2 = resultInfoIter.begin(); iter2 != resultInfoIter.end(); ++iter2) { const int startTime = iter2->first; const int endTime = iter2->second; std::string startTimeStr = startTime < 10 ? "0" : ""; std::string endTimeStr = endTime < 10 ? "0" : ""; startTimeStr += std::to_string(startTime); endTimeStr += std::to_string(endTime); cout << startTimeStr << "-" << endTimeStr << endl; } // 마지막 요소가 아니라면 구분선 출력 if (std::next(iter) != meetingRoomSet.end()) cout << "-----" << endl; } return 0; }
#[21년_재직자_대회_예선]_회의실_예약