출처
https://softeer.ai/practice/6266
문제 내용은 저작권 상 복붙하지 않았으니, 출처에서 확인!
풀이
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
string Room;
int N, M, startTime, endTime;
vector<string> Rooms(51);
map<string, set<int>> books;
void initBooks(string roomName) {
books.insert({ roomName, set<int>{} });
for (int i = 9; i < 18; i++) {
books[roomName].insert(i);
}
}
void confirmBook(string roomName, int startTime, int endTime) {
for (int i = startTime; i < endTime; i++) {
books[roomName].erase(i);
}
}
void printBooks(string roomName) {
cout << "Room " << roomName << ":" << endl;
if (books[roomName].size() == 0) {
cout << "Not available" << endl;
}
else {
vector<string> intervals;
auto it = books[roomName].begin();
int start = *it, prev = *it;
int cnt = 0;
++it;
while (it != books[roomName].end()) {
if (*it != prev + 1) {
intervals.push_back((start < 10 ? "0" : "") + to_string(start) + "-" + (prev + 1 < 10 ? "0" : "") + to_string(prev + 1));
start = *it;
cnt++;
}
prev = *it;
++it;
}
intervals.push_back((start < 10 ? "0" : "") + to_string(start) + "-" + (prev + 1 < 10 ? "0" : "") + to_string(prev + 1));
cnt++;
cout << cnt << " available:" << endl;
for (const auto& interval : intervals) {
cout << interval << endl;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> Rooms[i];
initBooks(Rooms[i]);
}
for (int i = 0; i < M; i++) {
cin >> Room >> startTime >> endTime;
confirmBook(Room, startTime, endTime);
}
sort(Rooms.begin(), Rooms.begin() + N);
for (int i = 0; i < N - 1; i++) {
printBooks(Rooms[i]);
cout << "-----" << endl;
}
printBooks(Rooms[N - 1]);
return 0;
}
과연 최적화된 코드일까? 고민해봐야 할 것 같다 '-'....
반응형
'알고리즘 > Softeer' 카테고리의 다른 글
[C++] Softeer Lv.3 나무 섭지 (0) | 2024.10.31 |
---|---|
[C++] Softeer Lv.2 GBC (0) | 2024.10.30 |
[C++] Softeer Lv.3 [HSAT 6회 정기 코딩 인증평가 기출] 출퇴근길 (0) | 2024.10.30 |
[C++] Softeer Lv.3 강의실 배정 (0) | 2024.10.29 |
[C++] Softeer Lv.2 [한양대 HCPC 2023] X marks the Spot (0) | 2024.10.28 |