코딩/SWEA

[SWEA] 2477. [모의 SW 역량테스트] 차량 정비소 C++

mjCodingLife 2024. 10. 10. 17:35

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV6c6bgaIuoDFAXy

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

#include <iostream>
#include <vector>
#include <unordered_set>
#include <queue>

using namespace std;
struct Person {
	int num;
	bool isDone;
};

int N, M, K, A, B;
vector<int> ai;
vector<int> bj;
vector<int> tk;

int main() {
	int T;
	cin >> T;
	for (int tc = 1; tc <= T; tc++) {
		cin >> N >> M >> K >> A >> B;
		ai.assign(N + 1, 0);
		bj.assign(M + 1, 0);
		tk.assign(K + 1, 0);
		vector<Person> people;
		people.push_back({ 0, true });
		for (int i = 1; i <= N; i++) {
			cin >> ai[i];
		}
		for (int j = 1; j <= M; j++) {
			cin >> bj[j];
		}
		for (int k = 1; k <= K; k++) {
			cin >> tk[k];
			people.push_back({ k, false });
		}

		vector<unordered_set<int>> a;
		vector<unordered_set<int>> b;
		a.assign(N + 1, unordered_set<int>());
		b.assign(M + 1, unordered_set<int>());
		
		queue<int> waitingQueueA;
		queue<int> waitingQueueB;
		vector<pair<int, int>> TimeA; // 고객 num, remain time
		vector<pair<int, int>> TimeB; // 고객 num, remain time
		TimeA.assign(N + 1, pair<int, int>({ 0, 0 }));
		TimeB.assign(M + 1, pair<int, int>({ 0, 0 }));
		// TODO
		int t = 0;
		int k = 1;

		while (true) {
			bool isDone = true;
			for (int i = 1; i < people.size(); i++) {
				if (people[i].isDone == false)
					isDone = false;
			}
			if (isDone) break;

			while ( k <= K && tk[k] == t)  {
				waitingQueueA.push(k);
				k++;
			}

			for (int i = 1; i <= N; i++) {
				if (TimeA[i].second != 0)
					TimeA[i].second--;
				if (TimeA[i].second == 0) {
					if(TimeA[i].first != 0)
						waitingQueueB.push(TimeA[i].first);
					TimeA[i].first = 0;
					if (!waitingQueueA.empty()) {
						TimeA[i].first = waitingQueueA.front();
						a[i].insert(TimeA[i].first);
						waitingQueueA.pop();
						TimeA[i].second += ai[i];
					}
				}
			}

			for (int i = 1; i <= M; i++) {
				if (TimeB[i].second != 0)
					TimeB[i].second--;
				if (TimeB[i].second == 0 ) {
					if (TimeB[i].first != 0)
						people[TimeB[i].first].isDone = true;
					TimeB[i].first = 0;
					if (!waitingQueueB.empty()) {
						TimeB[i].first = waitingQueueB.front();
						b[i].insert(TimeB[i].first);
						waitingQueueB.pop();
						TimeB[i].second += bj[i];
					}
				}
			}

			t++;
		}

		int result = 0;
		
		for (auto i : a[A]) {
			if (b[B].find(i) != b[B].end())
				result += i;
		}

		if (result == 0) result = -1;

		cout << "#" << tc << " " << result << endl;
	}
	return 0;
}