코딩공부/Softeer

[Softeer/C++] [21년 재직자 대회 예선] 회의실 예약

usingsystem 2024. 11. 5. 15:13
728x90

https://softeer.ai/practice/6266

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

string formatString(int time)
{
	string s = to_string(time);

	return s.size() < 2 ? "0" + s : s;
}

int main(int argc, char** argv)
{
	int n, m;

	cin >> n >> m;

	string in;
	int start, end;
	vector<string> roomName;

	map<string, vector<int>> room;
	for (int i = 0; i < n; i++)
	{
		cin >> in;
		roomName.push_back(in);
		room[in] = vector<int>(10);
	}
	for (int i = 0; i < m; i++)
	{
		cin >> in >> start >> end;

		for (int i = start - 9; i < end - 9; i++)
		{
			room[in][i] = 1;
		}

		if (end == 18)
			room[in][9] = 1;
	}

	//예약장소 sort
	sort(roomName.begin(), roomName.end());
	int count = 1;
	for (const auto& roomName : roomName)
	{
		cout << "Room " + roomName + ":\n";

		auto schedule = room[roomName];
		vector<string> availableTimes;
		int start = -1;
		int end = -1;

		for (int i = 0; i < schedule.size(); i++)
		{
			//예약가능한 시작시간을 발견
			if (schedule[i] == 0 && start == -1)
			{
				start = i + 9;
			}
			//예약된 시간을 발견
			else if (schedule[i] == 1 && start != -1)
			{
				end = i + 9;
				string s = formatString(start);
				string e = formatString(end);

				availableTimes.push_back(s + "-" + e);
				start = -1;
				end = -1;

			}
		}

		//예약가능 시작시간만 발견 예약된 시간을 발견 못함 그럼 18시까지 가능
		if (start != -1)
		{
			string s = formatString(start);
			string e = "18";

			availableTimes.push_back(s + "-" + e);
		}

		//출력
		{
			if (availableTimes.empty())
				cout << "Not available" << endl;
			else
			{
				cout << availableTimes.size() << " available:\n";
				for (const auto& time : availableTimes)
				{
					cout << time << endl;
				}
			}

			if (count != room.size())
				cout << "-----" << endl;
		}
		count++;
	}

	return 0;
}
728x90