Algorithm/problem
백준 1700번 : 멀티탭 스케줄링 - 그리디 C++
DingCoDing
2022. 3. 7. 15:34
반응형
https://www.acmicpc.net/problem/1700
1700번: 멀티탭 스케줄링
기숙사에서 살고 있는 준규는 한 개의 멀티탭을 이용하고 있다. 준규는 키보드, 헤어드라이기, 핸드폰 충전기, 디지털 카메라 충전기 등 여러 개의 전기용품을 사용하면서 어쩔 수 없이 각종 전
www.acmicpc.net
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
int n, k;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
vector<int> v;
for (int i = 0; i < k; i++) {
int tmp;
cin >> tmp;
v.push_back(tmp);
}
unordered_set<int> set;
int ans = 0;
for (int i = 0; i < k; i++) {
int tmp = v[i];
if (set.size() < n) {
set.insert(tmp);
}
else if(set.size()==n) {
if (set.count(tmp)) {
continue;
}
else {
map<int, int> m;
for (auto x : set) {
m[x] = 1000;
}
for (int j = i + 1; j < k; j++) {
if (set.count(v[j])) {
m[v[j]] = min(m[v[j]], j);
}
}
int maxx = 0;
int max_id = -1;
for (auto x : set) {
if (maxx < m[x]) {
maxx = m[x];
max_id = x;
}
}
set.erase(max_id);
set.insert(tmp);
ans++;
}
}
}
cout << ans << '\n';
return 0;
}
반응형