์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์คํ
- dynamic debugging
- ์ต๋จ ๊ฒฝ๋ก
- java
- ์์ ์ ๋ ฌ
- JPA
- ๊ตฌํ
- DP
- ๋ฐฑํธ๋ํน
- ๋์ ํฉ
- web
- c++
- ์ฐ์ ์์ ํ
- OS
- ์ด๋ถ ํ์
- Spring
- CVE
- ๋ถํ ์ ๋ณต
- BFS
- ์ฌ๊ท
- thymeleaf
- error
- Reversing
- GCP
- ์๋ฎฌ๋ ์ด์
- ๊ทธ๋ฆฌ๋
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๋งต
- dfs
- ๋ฌธ์์ด
- Today
- Total
hades
[Baekjoon] 30805๋ฒ: ์ฌ์ ์ ์ต๋ ๊ณตํต ๋ถ๋ถ ์์ด ๋ณธ๋ฌธ
[Baekjoon] 30805๋ฒ: ์ฌ์ ์ ์ต๋ ๊ณตํต ๋ถ๋ถ ์์ด
hades1 2024. 9. 9. 21:35๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/30805
๐ ์ค๊ณ
์ฌ์ ์์ผ๋กค ๊ฐ์ฅ ๋ค์ ์๋ ๊ณตํต ๋ถ๋ถ ์์ด์ ์ฐพ์ผ๋ฉด ๋๋ฏ๋ก, ๋จผ์ ๊ฐ ์์ด์ ํฐ ์๊ฐ ์์ ์ค๋๋ก, ์์ ํฌ๊ธฐ๊ฐ ๊ฐ๋ค๋ฉด, ์ธ๋ฑ์ค๊ฐ ์์ ๊ฒ์ด ์์ ์ค๋๋ก ํ๊ฒ ํ๊ธฐ ์ํด ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ์๋ค.
๊ฐ๊ฐ์ ์ฐ์ ์์ ํ์์ ์์ ์๋ ํ๋๋ฅผ ๊บผ๋ด๋ฉด์ ๊ฐ๋ค๋ฉด, ๊ฒฐ๊ณผ์ ์ถ๊ฐํ๋ค.
๋ค๋ฅด๋ค๋ฉด, ํฐ ๊ฒ์ด ์๋ ์ฐ์ ์์ ํ์์ ํ๋๋ฅผ ์์ค๋ค. ํฐ ๊ฒ์ด ๊ณ์ ๋จ์์๋ค๋ฉด, ๊ทธ์ ๊ฐ์ ๊ฒ์ด ๋ค๋ฅธ ์ชฝ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์ฃผ์ํด์ผํ ๊ฒ์ ํฌ๊ธฐ ๋ฟ๋ง ์๋๋ผ ๊ฒฐ๊ณผ์ ํฌํจ๋๋ ์๊ฐ ๊ฐ๊ฐ ์์ชฝ ์์ด์์ ์ด๋ ์ธ๋ฑ์ค์ ์์นํ๋ ์ง๋ฅผ ๊ธฐ๋กํ๊ณ , ๊ทธ ์ธ๋ฑ์ค๋ณด๋ค ์๋ค๋ฉด ๊ฒฐ๊ณผ์ ์ถ๊ฐ๋ ์ ์์ผ๋ฏ๋ก ์์ ๋ ๊ณผ์ ์ด ํฌํจ๋์ด์ผ ํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, number, limit_a_idx = 0, limit_b_idx = 0;
struct compare {
bool operator()(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
};
priority_queue<pair<int, int>, vector<pair<int, int>>, compare> pq1;
priority_queue<pair<int, int>, vector<pair<int, int>>, compare> pq2;
vector<int> result;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> number;
pq1.push({ number, i });
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> number;
pq2.push({ number, i });
}
while (!pq1.empty() && !pq2.empty()) {
if (!pq1.empty() && pq1.top().second < limit_a_idx) {
pq1.pop();
continue;
}
if (!pq2.empty() && pq2.top().second < limit_b_idx) {
pq2.pop();
continue;
}
if (pq1.top().first == pq2.top().first) {
result.push_back(pq1.top().first);
limit_a_idx = pq1.top().second;
limit_b_idx = pq2.top().second;
pq1.pop();
pq2.pop();
}
else if (pq1.top().first > pq2.top().first) {
pq1.pop();
}
else if (pq1.top().first < pq2.top().first) {
pq2.pop();
}
}
int size = result.size();
cout << size << "\n";
for (int i = 0; i < size; i++) {
cout << result[i] << " ";
}
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 6198๋ฒ: ์ฅ์ ์ ์ ๊พธ๋ฏธ๊ธฐ (0) | 2024.09.13 |
---|---|
[Baekjoon] 15486๋ฒ: ํด์ฌ 2 (0) | 2024.09.10 |
[Baekjoon] 16236๋ฒ: ์๊ธฐ ์์ด (0) | 2024.09.07 |
[Baekjoon] 5639๋ฒ: ์ด์ง ๊ฒ์ ํธ๋ฆฌ (0) | 2024.09.06 |
[Baekjoon] 17144๋ฒ: ๋ฏธ์ธ๋จผ์ง ์๋ ! (0) | 2024.09.05 |