[BOJ] 1700 멀티탭 스케줄링

Time Lapse :25min 23sec

1700.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <memory.h>
int use[100];
int mtap[100];
int holes, K, ans = 0;
void OPT() {
int predict[101];
int hole = 0;
for (int i = 0; i < K; ++i) {
int flag = 0;
for (int j = 0; j < hole; ++j)
if (mtap[j] == use[i]) { flag = 1; break; }
if (flag) continue;
if (hole < holes) {
mtap[hole] = use[i];
++hole;
}
else {
memset(predict, 0, sizeof(int)*(K+1));
for (int j = i; j < K; ++j) {
if (predict[use[j]]==0) predict[use[j]] = j;
}
int umax = 0;
for (int j = 0; j < hole; ++j) {
if (!predict[mtap[j]]) { flag = j; break; }
else if (umax < predict[mtap[j]]) {
umax = predict[mtap[j]];
flag = j;
}
}
++ans;
mtap[flag] = use[i];
}
}
}
int main() {
scanf("%d %d", &holes, &K);
for (int i = 0; i < K; ++i)
scanf("%d", &use[i]);
OPT();
printf("%d", ans);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
#include <vector>
#include <unordered_set>

using namespace std;

vector<vector<int>> use(101, vector<int>());
vector<int> seq(101);
int N, K;

int solution() {
if(N >= K) return 0;

int res = 0, idx = 0;
unordered_set<int> inUsing;

while(inUsing.size() != N) inUsing.insert(seq[idx++]);
for(; idx < K; idx++) {
if(inUsing.count(seq[idx])) continue;

int removeItem = -1;
int removeTime = -1;

for(auto useItem : inUsing) {
auto it = lower_bound(use[useItem].begin(), use[useItem].end(), idx);
if(it == use[useItem].end()) {
removeItem = useItem; break;
}

if(removeTime < *it) {
removeTime = *it;
removeItem = useItem;
}
}

inUsing.erase(removeItem);
inUsing.insert(seq[idx]);
res++;
}

return res;
}
int main() {
int item;
scanf("%d %d",&N, &K);
for(int i = 0; i < K; i++) {
scanf("%d", &seq[i]);
use[seq[i]].push_back(i);
}
cout<<solution();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/1700/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.