๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[C++/SWEA] 1859. ๋ฐฑ๋งŒ ์žฅ์ž ํ”„๋กœ์ ํŠธ (D2)

728x90

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV5LrsUaDxcDFAXc&categoryId=AV5LrsUaDxcDFAXc&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=CCPP&select-1=3&pageSize=10&pageIndex=1

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

์•„๋งˆ ๊ทธ๋ฆฌ๋””? ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค

๋ฐฐ์—ด์„ ์—ญ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ 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