diff --git a/kangrae-jo/README.md b/kangrae-jo/README.md index e70a2b0..c83b4c0 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -34,3 +34,4 @@ | 30차시 | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)| | 31차시 | 2024.07.31 | Prefix Sum | [두 배열의 합](https://www.acmicpc.net/problem/2143)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/122)| | 32차시 | 2024.08.10 | BFS | [퍼즐 조각 채우기](https://school.programmers.co.kr/learn/courses/30/lessons/84021)|[#130](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/130)| +| 33차시 | 2024.08.18 | UnionFind | [카드 게임](https://www.acmicpc.net/problem/16566)|[#133](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/133)| diff --git a/kangrae-jo/Two Pointer/14-kangrae-jo.cpp b/kangrae-jo/TwoPointer/14-kangrae-jo.cpp similarity index 100% rename from kangrae-jo/Two Pointer/14-kangrae-jo.cpp rename to kangrae-jo/TwoPointer/14-kangrae-jo.cpp diff --git a/kangrae-jo/UnionFind/33-kangrae-jo.cpp b/kangrae-jo/UnionFind/33-kangrae-jo.cpp new file mode 100644 index 0000000..1919228 --- /dev/null +++ b/kangrae-jo/UnionFind/33-kangrae-jo.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +using namespace std; + +struct DSU { + vector p; + DSU(int n, int s) : p(n) { iota(p.begin(), p.end(), s); } + int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } + void use(int x) { p[x] = find(x + 1); } +}; + +int main() { + ios_base::sync_with_stdio(0); + cout.tie(0); + cin.tie(0); + + int N, M, K; + cin >> N >> M >> K; + + vector blues(M); + for (int i = 0; i < M; i++) { + cin >> blues[i]; + } + sort(blues.begin(), blues.end()); + + DSU dsu(M + 1, 0); + while (K--) { + int red; + cin >> red; + + int index = upper_bound(blues.begin(), blues.end(), red) - blues.begin(); + index = dsu.find(index); + cout << blues[index] << '\n'; + + dsu.use(index); + } + + return 0; +} \ No newline at end of file