728x90
์๋ง ๊ทธ๋ฆฌ๋? ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค
๋ฐฐ์ด์ ์ญ์์ผ๋ก ํ์ํ๋ฉด์ max๊ฐ์ ๊ฐฑ์ ํ๊ณ , ๊ทธ๋ณด๋ค ์์ ๊ฐ๋ค์ ๋นผ์ฃผ๋ฉด ๋๋ค (ํ๋งค ํ ์ฐจ์ต)
๋์ ํ์ด
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int test_case;
int T;
int n;
//freopen("input.txt", "r", stdin);
cin >> T;
for(test_case = 1; test_case <= T; ++test_case)
{
vector<long long> v;
long long num = 0;
long long answer = 0;
int maxprice = 0;
cin >> n;
for (int i = 0; i < n; ++i){
cin >> num;
v.push_back(num);
}
maxprice = v[n - 1];
for (int i = n - 2; i >= 0; --i)
{
if(maxprice > v[i]){
answer += (maxprice - v[i]);
}
else{
maxprice = v[i];
}
}
cout << "#" << test_case << " " << answer << "\n";
}
return 0;
}
728x90
'๐ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++/SWEA] 1208. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - Flatten (D3) (0) | 2024.11.16 |
---|---|
[C++/SWEA] 21425. += (D2) (0) | 2024.11.16 |
[C++/SWEA] 1215. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 3์ผ์ฐจ - ํ๋ฌธ1 (D3) (0) | 2024.11.16 |
[C++/SWEA] 1206. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - View (D3) (0) | 2024.11.16 |