๐Ÿ’ญ Problem Solving

[BOJ][C++] ๋ฐฑ์ค€ 20546๋ฒˆ: ๐Ÿœ ๊ธฐ์ ์˜ ๋งค๋งค๋ฒ• ๐Ÿœ

0=2. 2024. 5. 26. 17:04

๋ฌธ์ œ

https://www.acmicpc.net/problem/20546

ํ’€์ด

์„ฑ๋ฏผ(BNP)

  • ๋‚จ์€ ํ˜„๊ธˆ์œผ๋กœ ์ฃผ์‹์„ ์‚ด ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ์ฃผ์‹ ๋งค์ˆ˜

์ค€ํ˜„(TIMING)

  • 3์ผ ์—ฐ์† ๊ฐ€๊ฒฉ ์ƒ์Šน: ์ „๋Ÿ‰ ๋งค๋„
  • 3์ผ ์—ฐ์† ๊ฐ€๊ฒฉ ํ•˜๋ฝ: ์ „๋Ÿ‰ ๋งค์ˆ˜
  • ๋งค์ˆ˜ํ•  ๋•Œ๋Š” (์„ฑ๋ฏผ๊ณผ ๋˜‘๊ฐ™์ด) ๋‚จ์€ ํ˜„๊ธˆ์œผ๋กœ ์ฃผ์‹์„ ์‚ด ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋งค์ˆ˜

์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

const int DAY = 14;

int bnp(int cash, vector<int> &price) {
    int stock = 0;

    for (int i = 0; i < DAY; i++) {
        while (cash - price[i] >= 0) {
            cash -= price[i];
            stock++;
        }
    }

    return stock * price[DAY - 1] + cash;
}

int timing(int cash, vector<int> &price) {
    int stock = 0, up = 0, down = 0;

    for (int i = 1; i < DAY; i++) {
        // ๊ฐ€๊ฒฉ ์ƒ์Šน, ํ•˜๋ฝ ์ฒดํฌ
        if (price[i - 1] < price[i]) {  // ๊ฐ€๊ฒฉ ์ƒ์Šน
            up++;
            down = 0;
        } else if (price[i - 1] > price[i]) {   // ๊ฐ€๊ฒฉ ํ•˜๋ฝ
            down++;
            up = 0;
        } else {    // ์ฃผ๊ฐ€ ๋™์ผ
            up = 0;
            down = 0;
        }

        // ๋งค๋„, ๋งค์ˆ˜
        if (up == 3) { // 3์ผ ์—ฐ์† ๊ฐ€๊ฒฉ ์ƒ์Šน
            while (stock) {  // ์ „๋Ÿ‰ ๋งค๋„
                cash += price[i];
                stock--;
            }
            up = 0;
        } else if (down == 3) {  // 3์ผ ์—ฐ์† ๊ฐ€๊ฒฉ ํ•˜๋ฝ
            while (cash - price[i] >= 0) {  // ์ „๋Ÿ‰ ๋งค์ˆ˜
                cash -= price[i];
                stock++;
            }
            down = 0;
        }
    }

    return stock * price[DAY - 1] + cash;
}

int main() {
    int cash, bnp_result, timing_result;
    vector<int> price(DAY, 0);

    // ์ž…๋ ฅ
    cin >> cash;
    for (int &i: price) {
        cin >> i;
    }

    // ์—ฐ์‚ฐ
    bnp_result = bnp(cash, price);
    timing_result = timing(cash, price);

    // ์ถœ๋ ฅ
    if (bnp_result > timing_result)
        cout << "BNP";
    else if (bnp_result < timing_result)
        cout << "TIMING";
    else
        cout << "SAMESAME";

    return 0;
}