주식 - 11501

2025. 2. 13. 09:55PS/백준

문제

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

느낀점

dp, 주식과 같은 문제를 풀 때, 뒤에서 생각해보는 것도 방법이다

풀이

#include <iostream>
#include <vector>

int main()
{
    int T, N, price, base = 0;
    bool asc;
    std::vector<int> priceList;
    std::cin >> T;
    for (int i = 0; i < T; i++)
    {
        long answer = 0;
        int maxValue = 0;
        priceList.clear();
        std::cin >> N;

        for (int j = 0; j < N; j++)
        {
            std::cin >> price;
            priceList.push_back(price);
        }

        for (int k = N - 1; k >= 0; k--)
        {
            if (priceList[k] >= maxValue)
            {
                maxValue = priceList[k];
            }
            else
            {
                answer += (maxValue - priceList[k]);
            }
        }
        std::cout << answer << "\n";
    }
    return 0;
}

'PS > 백준' 카테고리의 다른 글

가장 큰 증가하는 부분 수열 - 11055  (0) 2025.02.13
점프 - 1890  (0) 2025.02.13
한 줄로 서기 - 1138  (0) 2025.02.12
미로 만들기 - 1347  (0) 2025.02.12
창고 다각형 - 2304  (0) 2025.02.12