[BOJ] 13335๋ฒˆ | ํŠธ๋Ÿญ (C++)

2023. 9. 4. 21:20ใ†Coding Test/BOJ

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

13335๋ฒˆ: ํŠธ๋Ÿญ

์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” ํ‘œ์ค€์ž…๋ ฅ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ž…๋ ฅ์€ ๋‘ ์ค„๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ n (1 ≤ n ≤ 1,000) , w (1 ≤ w ≤ 100) and L (10 ≤ L ≤ 1,000)์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, n์€ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

  1. ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๊ธฐ ์œ„ํ•ด ๋Œ€๊ธฐ ์ค‘์ธ ํŠธ๋Ÿญ๋“ค, ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ์ค‘์ธ ํŠธ๋Ÿญ๋“ค์„ ๊ด€๋ฆฌํ•  2๊ฐœ์˜ ํ(Queue)๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
  2. ํ•ด๋‹น 2๊ฐœ์˜ ํ๊ฐ€ ๋ชจ๋‘ ๋น„์—ˆ๋‹ค๋ฉด, ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
  3. ์•„๋‹ˆ๋ผ๋ฉด, ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ๋Ÿญ๋“ค ์ค‘ ๋งจ ์•ž ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„œ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    • "๊ฒฝ๊ณผ์‹œ๊ฐ„ - ํ•ด๋‹น ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ์— ์ง„์ž…ํ•œ ์‹œ๊ฐ = ๋‹ค๋ฆฌ์˜ ๊ธธ์ด"๋ผ๋ฉด, ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„œ์Šต๋‹ˆ๋‹ค.
      • ์ด ๊ฒฝ์šฐ, ์ด ๋ฌด๊ฒŒ์—์„œ ํ•ด๋‹น ํŠธ๋Ÿญ ๋ฌด๊ฒŒ๋ฅผ ๋นผ์ฃผ๊ณ , ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ๋Ÿญ๋“ค ํ์—์„œ pop ํ•ด์ค๋‹ˆ๋‹ค.
  4. "์ด ๋ฌด๊ฒŒ + ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ ๋‹ค์Œ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ"๊ฐ€ ๋‹ค๋ฆฌ์˜ ์ตœ๋Œ€ ํ•˜์ค‘ ์ดํ•˜์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    • ์ตœ๋Œ€ ํ•˜์ค‘ ์ดํ•˜๋ผ๋ฉด, ํ•ด๋‹น ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ๋Ÿญ๋“ค ํ์— ๋„ฃ์–ด์ฃผ๊ณ , ๋Œ€๊ธฐ ํŠธ๋Ÿญ ํ์—์„œ pop ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด ๋ฌด๊ฒŒ์— ํ•ด๋‹น ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋”ํ•ด์ค๋‹ˆ๋‹ค.
  5. ๊ฒฝ๊ณผ์‹œ๊ฐ„์„ +1 ํ•ฉ๋‹ˆ๋‹ค.
  6. 2๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๊นŒ์ง€, 3~5๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <queue>
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using namespace std;

struct Truck
{
    Truck() { }
    Truck(int weight, int enterTime) : _weight(weight), _enterTime(enterTime){ }
    int _weight;
    int _enterTime;
};

void CheckTheTruckOnBridge(queue<Truck>& trucks, const int totalTime, const int bridgeLength, int& currentWeight)
{
    // ๋‹ค๋ฆฌ ์œ„์˜ ํŠธ๋Ÿญ๋“ค ์ค‘ ๋งจ ์•ž ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๋‹ค ์ง€๋‚ฌ๋Š”์ง€ ์ฒดํฌ
    while (!trucks.empty())
    {
        Truck frontTruck = trucks.front();

        // ๋‹ค๋ฆฌ๋ฅผ ๋‹ค ๊ฑด๋„Œ ๊ฒฝ์šฐ, ์ด ๋ฌด๊ฒŒ์—์„œ ํ•ด๋‹น ํŠธ๋Ÿญ ๋ฌด๊ฒŒ ๋นผ๊ธฐ
        if (totalTime - frontTruck._enterTime == bridgeLength)
        {
            currentWeight -= frontTruck._weight;
            trucks.pop();
        }

        break;
    }
}

int main()
{
    FAST_IO

    int truckNums, bridgeLength, bridgeMaxWeight;
    cin >> truckNums >> bridgeLength >> bridgeMaxWeight;

    queue<int> trucks;
    for (int i = 0; i < truckNums; i++)
    {
        int weight;
        cin >> weight;
        trucks.emplace(weight);
    }

    queue<Truck> trucksOnBridge;
    int currentWeight = 0, totalTime = 0;

    while (true)
    {
        if (trucks.empty() and trucksOnBridge.empty())
            break;

        CheckTheTruckOnBridge(trucksOnBridge, totalTime, bridgeLength, currentWeight);

        // ๋‹ค๋ฆฌ ์œ„์— ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒฝ์šฐ ์˜ฌ๋ฆฌ๊ธฐ
        if (!trucks.empty() and currentWeight + trucks.front() <= bridgeMaxWeight)
        {
            int currentTruckWeight = trucks.front();
            trucksOnBridge.push(Truck(currentTruckWeight, totalTime));

            currentWeight += currentTruckWeight;
            trucks.pop();
        }

        totalTime++;
    }

    cout << totalTime;
    return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•