하루하나코딩

백준 2096 : 내려가기 c++

HAHAKO 2023. 1. 26. 13:07

코드

#include <iostream>
#include <algorithm>

using namespace std;

int maxdp[3] = {0, 0, 0};
int mindp[3] = {0, 0, 0};
int num[3];

int main(){
	
	int n;
	cin >> n;
	int maxtemp[3];
	int mintemp[3];
		
	for(int i = 0; i < n; i++){
		for(int j = 0; j < 3; j++){
			cin >> num[j];
			maxtemp[j] = maxdp[j];
			mintemp[j] = mindp[j];
		}
		maxdp[0] = max(maxtemp[0], maxtemp[1]) + num[0];
		maxdp[1] = max(max(maxtemp[0], maxtemp[1]), maxtemp[2]) + num[1];
		maxdp[2] = max(maxtemp[1], maxtemp[2]) + num[2];
		mindp[0] = min(mintemp[0], mintemp[1]) + num[0];
		mindp[1] = min(min(mintemp[0], mintemp[1]), mintemp[2]) + num[1];
		mindp[2] = min(mintemp[1], mintemp[2]) + num[2];
	}
	
	cout << max(max(maxdp[0], maxdp[1]), maxdp[2]) << " " << min(min(mindp[0], mindp[1]), mindp[2]);

	return 0;
}

알게된 점

처음에 숫자랑 dp배열 [100001][3] 으로 선언해서 쉽게짰더니 바로 메모리초과나와서

num 받을때마다 갱신하게끔 짜니까 통과했네용.

temp를 선언해서 따로 저장해놓는 센스가 중요한 듯 합니다!