알고리즘/SW Expert Academy

[SWEA] 1859. 백만장자 프로젝트 (D2) / C++

minari 2023. 11. 1. 12:41

https://swexpertacademy.com/main/code/problem/problemDetail.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<vector>
using namespace std;
 
int main(int argc, char** argv)
{
    int test_case;
    int T;
 
    //freopen("input.txt", "r", stdin);
    cin >> T;
    /*
       여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
    */
    vector<int> wholePrice;
 
    for (test_case = 1; test_case <= T; ++test_case)
    {
 
        /////////////////////////////////////////////////////////////////////////////////////////////
        /*
             이 부분에 여러분의 알고리즘 구현이 들어갑니다.
         */
         /////////////////////////////////////////////////////////////////////////////////////////////
        long result = 0;
        wholePrice.clear();
 
         // 각 테스트케이스 별로 자연수 N이 주어진다
        int N; 
        cin >> N;
 
        // 각 날의 매매가 주어진다
        for (int i = 0; i < N; i++)
        {
            int price;
            cin >> price;
            wholePrice.push_back(price);
        }        
 
        // 가장 큰 값과, 그 인덱스를 저장
        long maxPrice = 0, index = 0, maxIndex = 0;
 
        while (index < N)
        {
            maxPrice = 0;
            for (int i = index; i < N; i++)
            {
                if (maxPrice < wholePrice[i])
                {
                    maxPrice = wholePrice[i];
                    maxIndex = i;
                }
            }
 
            // 큰 값 기준, 앞의 날은 모두 구매한 뒤, 큰 값의 날에 판다
            int count = maxIndex - index;
            if (count > 0)
            {
                long buy = 0;
                for (int k = index; k < maxIndex; k++)
                    buy += wholePrice[k];
 
                result += (wholePrice[maxIndex] * count - buy);
            }
            index = maxIndex + 1;
        }
 
        cout << "#" << test_case << " " << result << "\n";
    }
    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
 
// 1트: 아놔 7/10
// 2트: int에서 long으로 바꾸어주니 OK
cs