Skip to content

Commit 6f254d0

Browse files
committed
Update 1700.cpp
1 parent 6d6168d commit 6f254d0

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

0x11/solutions/1700.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,52 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : sukam09
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/03e21675c9ef49a8b6f1ee6838a44aeb
44
#include <bits/stdc++.h>
55
using namespace std;
66

7+
#define X first
8+
#define Y second
9+
10+
int a[105]; // 전기용품의 사용 순서
11+
bool power[105]; // 해당 전기용품이 멀티탭에 꽂혀 있는가?
12+
713
int main(void){
814
ios::sync_with_stdio(0);
915
cin.tie(0);
1016

17+
int n, k;
18+
cin >> n >> k;
19+
for (int i = 1; i <= k; i++) cin >> a[i];
20+
int ans = 0;
21+
int cnt = 0; // 멀티탭에 꽂혀 있는 전기용품의 개수
22+
for (int i = 1; i <= k; i++) {
23+
int cur = a[i];
24+
if (power[cur]) continue; // 이미 꽂혀 있으면 넘어감
25+
// 멀티탭에 자리가 남으면 그냥 꽂음
26+
if (cnt < n) {
27+
power[cur] = true;
28+
cnt++;
29+
}
30+
else {
31+
// 멀티탭에 꽂혀 있는 전기용품 중 a에서 앞으로 가장 빨리 나올 위치를 이름과 함께 저장함
32+
vector<pair<int, int>> idx;
33+
for (int x = 1; x <= k; x++) {
34+
if (!power[x]) continue;
35+
bool vis = false;
36+
for (int y = i + 1; y <= k; y++) {
37+
if (a[y] == x) {
38+
idx.push_back({y, x});
39+
vis = true;
40+
break;
41+
}
42+
}
43+
if (!vis) idx.push_back({k + 1, x}); // a에서 나오지 않으면 k + 1로 처리
44+
}
45+
sort(idx.begin(), idx.end(), greater<pair<int, int>>());
46+
int target = idx[0].Y; // 가장 늦게 사용할 전기용품을 뽑고 cur을 꽂으면 됨
47+
power[target] = false; ans++;
48+
power[cur] = true;
49+
}
50+
}
51+
cout << ans;
1152
}

0 commit comments

Comments
 (0)