728x90
๋ฌธ์ & ์ค๋ช
https://www.acmicpc.net/problem/1269
ํ์ด
A์ ์์ ํ๋ํ๋์ ๊ฐ์ B์์ ์ฐพ์ ๋ ์ด๋ถํ์์ ์งํํ๋ค.
์ ๋ต ์ฝ๋
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int aNum, bNum, input, sum = 0;
int l, h, mid; // ์ด๋ถํ์์ ์ํ ๋ณ์
vector<int> As, Bs;
// Input
cin >> aNum >> bNum;
for (int i = 0; i < aNum; i++) {
cin >> input;
As.push_back(input);
}
for (int i = 0; i < bNum; i++) {
cin >> input;
Bs.push_back(input);
}
sort(As.begin(), As.end());
sort(Bs.begin(), Bs.end());
// Search
for (int i = 0; i < aNum; i++) {
l = 0;
h = bNum;
while (l <= h) {
mid = (l + h) / 2;
if (As[i] == Bs[mid]) {
As[i] = Bs[mid] = 0;
break;
}
else if (As[i] > Bs[mid]) l = mid + 1;
else h = mid - 1;
}
}
// Count & Output
for (int i = 0; i < aNum; i++) {
if (As[i] > 0) sum += 1;
}
for (int i = 0; i < bNum; i++) {
if (Bs[i] > 0) sum += 1;
}
cout << sum << "\n";
return 0;
}
์๊ฐ์ด๊ณผ ์ฝ๋
๋๋ณด๊ธฐ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int aNum, bNum, input, searchIndex = 0, sum = 0;
vector<int> As, Bs;
// Input
cin >> aNum >> bNum;
for (int i = 0; i < aNum; i++) {
cin >> input;
As.push_back(input);
}
for (int i = 0; i < bNum; i++) {
cin >> input;
Bs.push_back(input);
}
sort(As.begin(), As.end(), greater<int>());
sort(Bs.begin(), Bs.end(), greater<int>());
// Search
searchIndex = 0;
for (int i = 0; i < aNum; i++) {
for (int j = searchIndex; j < bNum; j++) {
if (As[i] > Bs[j]) break;
if (As[i] == Bs[j]) {
As[i] = Bs[j] = 0;
searchIndex = j + 1;
break;
}
}
}
// Count & Output
for (int i = 0; i < aNum; i++) {
if (As[i] > 0) sum += 1;
}
for (int i = 0; i < bNum; i++) {
if (Bs[i] > 0) sum += 1;
}
cout << sum << "\n";
return 0;
}
728x90
'๐ Coding Test Study > Algorithm Problem' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ][C++] 1463๋ฒ 1๋ก ๋ง๋ค๊ธฐ(feat. DP) (0) | 2024.06.07 |
---|---|
[C++][BOJ] 2343 ๊ธฐํ ๋ ์จ (feat. ์ด๋ถํ์) (0) | 2024.05.23 |
[C++][BOJ] 2792๋ฒ ๋ณด์ ์์(feat. ์ด๋ถ ํ์) (0) | 2024.05.21 |
[C++][BOJ] 9935 ๋ฌธ์์ด ํญ๋ฐ :: ๋ฌธ์์ด ๋น๊ต(stack, erase) (0) | 2024.05.15 |
[C++][BOJ] 1931 ํ์์ค ๋ฐฐ์ :: ๋ผ์ธ ์ค์ํ (0) | 2024.05.15 |