[BOJ] 1700번 : 멀티탭 스케줄링

2024. 9. 9. 18:23Algorithm

1. problem : 

https://www.acmicpc.net/problem/1700

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;
int n, k; 
vector<int> orders; 
bool powers[105];
int ans;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	for (int i = 0; i < k; i++) {
		int x;
		cin >> x;
		orders.push_back(x);
	}
	int cnt = 0; 
	for (int i = 0; i < k; i++) {
		if (powers[orders[i]-1]) continue; 
		if (cnt < n) {
			powers[orders[i]-1] = true;
			cnt++;
		}
		else {
			vector<pair<int, int>> idx; 
			for (int j = 0; j < k; j++) {
				if (!powers[j]) continue; 
				bool find = false; 
				for (int m = i + 1; m < k; m++) {
					if (orders[m] == j + 1) {
						idx.push_back({ m,j}); //m번째에서 발견했습니다. j라는 값을
						find = true; 
						break;
					}
				}
				if (!find) idx.push_back({ k + 1,j });
			}
			sort(idx.begin(), idx.end(), greater<pair<int, int>>());
			int target = idx[0].second; 
			powers[target] = false; 
			ans++; 
			powers[orders[i] - 1] = true;
		}
	}
	cout << ans;
}

source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x11/solutions/1700.cpp

 

basic-algo-lecture/0x11/solutions/1700.cpp at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

제로 인덱스로 풀려하니, 많이 헷갈렸다. 내가 본 코드를 기반으로 작성했다. 다음부터는 주어진 인덱스로 풀 수 있으면 풀어야겠다. 또한, bool power [] 배열을 설정해 , 전원을 관리하는 게 정말 멋졌다. 이중 for문을 이용해 접근하려고 했다. 

'Algorithm' 카테고리의 다른 글

[BOJ] 15684번 : 사다리 조작  (0) 2024.09.11
[BOJ] 14890번 : 경사로  (0) 2024.09.10
[BOJ] 2170번 : 선 긋기  (0) 2024.09.08
[BOJ] 1744번 : 수 묶기  (0) 2024.09.08
[BOJ] 11501번 : 주식  (2) 2024.09.08