๊ณจ๋2 : ๊ทธ๋ฆฌ๋, ์ ๋ ฌ ๋ฌธ์ ์ด๋ค.
์๊ฐ
์ค์ ๋ก ์ด์ก๋ฌผ์ ์์ง์ธ๋ค๊ณ ์๊ฐํด๋ณด์. ๊ทธ๋ ๋ค๋ฉด ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์์ง์ผ ์ ์๋ ํฌ๋ ์ธ์ด ๊ฐ์ฅ ๋ง์ด ์์ง์ฌ์ผ ์ต๋จ ์๊ฐ์ ์ง์ ์์ง์ผ ์ ์๋ค. ์ด๋ถ๋ถ์ด ํต์ฌ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์ง์ ์์ง์ผ ์ ์๋ ๊ฒฝ์ฐ๋, ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์์ง์ผ ์ ์๋ ํฌ๋ ์ธ์ด ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ๋ชป์ฎ๊ธธ ๋์ด๋ค.
์๊ณ ๋ฆฌ์ฆ
- ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์ฎ๊ธธ ์ ์๋ ์์๋ก ์ ๋ ฌํ๋ค.
- ํ์ค๋ ๋ฌด๊ฑฐ์ด ํ์ค๋ถํฐ ์ ๋ ฌํ๋ค.
- ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์ฎ๊ธธ ์ ์๋ ํฌ๋ ์ธ์ด ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์ฎ๊ธธ ์ ์๋์ง ํ์ธํ๋ค.
- ์ฎ๊ธธ ์ ์๋ค๋ฉด -1์ ์ถ๋ ฅํ๋ค.
- ์ฎ๊ธธ ์ ์๋ค๋ฉด ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ํ์ค์ ์ฎ๊ธฐ๋ ํฌ๋ ์ธ ๋ถํฐ ๋ฌด๊ฑฐ์ด ํ์ค ๋ถํฐ ์ฎ๊ธด๋ค.
- ์ด ๊ณผ์ ์ ๋ชจ๋ ํ์ค์ ์ฎ๊ธธ ๋๊น์ง ๋ฐ๋ณตํ๊ณ ๊ทธ ๋๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ถ๋ ฅํ๋ค.
Code
#include <iostream>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <vector>
using namespace std;
int N, M;
int crane[51];
vector<int> box;
int main(){
cin >> N;
for (int i = 0; i < N; i++) {
cin >> crane[i];
}
cin >> M;
for (int i = 0; i < M; i++) {
int temp;
cin >> temp;
box.push_back(temp);
}
sort(crane, crane+N, greater<>());
sort(box.begin(), box.end(), greater<>());
if (box[0] > crane[0]) {
cout << -1 << '\n';
return 0;
}
int loaded = 0, index = 0, count = 0;
while (loaded != int(box.size())) {
for (int i = 0; i < M; i++) {
if (crane[index] >= box[i] && box[i] != 0) {
box[i] = 0;
loaded++;
index++;
}
if (index == N) break;
}
count++;
index = 0;
}
cout << count << '\n';
return 0;;
}