본문 바로가기
알고리즘/알고리즘

[C++] 백준 20413번 : MVP 다이아몬드 (Easy)

by 두둠칫 2022. 5. 8.

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

 

20413번: MVP 다이아몬드 (Easy)

입력된 MVP 등급을 달성하기 위한 최대 누적 과금액을 만원 단위로 출력한다.

www.acmicpc.net

 

그리디 알고리즘 적용하는 기본 구현 문제

 

#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <set>
#include <sstream>

using namespace std;

int N, b = 0, s, g, p, d, pay[36];
char grd;
int ans = 0;

int cal(int idx, char C) {
	if (idx == 0) {
		if (C == 'B')
			return s-1;
		else if (C == 'S')
			return g-1;
		else if (C == 'G')
			return p-1;
		else if (C == 'P')
			return d-1;
		else if (C == 'D')
			return d;
	}
	else {
		int pPay = pay[idx - 1];

		if (C == 'B')
			return s - 1 - pPay;
		else if (C == 'S')
			return g - 1 - pPay;
		else if (C == 'G')
			return p - 1 - pPay;
		else if (C == 'P')
			return d - 1 - pPay;
		else if (C == 'D')
			return d;
	}

}

int main() {

	ios::sync_with_stdio(0);
	cout.tie(0);

	cin >> N >> s >> g >> p >> d;
	for (int i = 0; i < N; i++) {
		cin >> grd;
		pay[i] = cal(i, grd);
		ans += pay[i];
	}

	cout << ans;

	return 0;
}