๊ณจ๋“œ2 : ๊ทธ๋ฆฌ๋””, ์ •๋ ฌ ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

์‹ค์ œ๋กœ ์šด์†ก๋ฌผ์„ ์›€์ง์ธ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ํฌ๋ ˆ์ธ์ด ๊ฐ€์žฅ ๋งŽ์ด ์›€์ง์—ฌ์•ผ ์ตœ๋‹จ ์‹œ๊ฐ„์— ์ง์„ ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ถ€๋ถ„์ด ํ•ต์‹ฌ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ง์„ ์›€์ง์ผ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š”, ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ํฌ๋ ˆ์ธ์ด ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ๋ชป์˜ฎ๊ธธ ๋•Œ์ด๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  2. ํ•˜์ค‘๋„ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘๋ถ€ํ„ฐ ์ •๋ ฌํ•œ๋‹ค.
  3. ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š” ํฌ๋ ˆ์ธ์ด ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  4. ์˜ฎ๊ธธ ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  5. ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘์„ ์˜ฎ๊ธฐ๋Š” ํฌ๋ ˆ์ธ ๋ถ€ํ„ฐ ๋ฌด๊ฑฐ์šด ํ•˜์ค‘ ๋ถ€ํ„ฐ ์˜ฎ๊ธด๋‹ค.
  6. ์ด ๊ณผ์ •์„ ๋ชจ๋“  ํ•˜์ค‘์„ ์˜ฎ๊ธธ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ  ๊ทธ ๋•Œ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

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;;
 
}
 

Reference