백준 9465 스티커

CJB_ny·2022년 12월 19일
0

백준

목록 보기
10/104
post-thumbnail

분석

일단 또 노가다를 하였지만 1시간 초과했기 때문에

https://omyodevelop.tistory.com/51
이사람 코드를 보면서 이해를 할려고 노력을함.. 그래도 잘 안되었지만

이런식으로 다 적어가면서 코드 번갈아 보니까

아래 코드가 무슨말을 하는지 어느정도는 이해되어서 맞춤.

cache[0][c] = max(cache[1][c-1], cache[1][c-2]) + map[0][c];
cache[1][c] = max(cache[0][c - 1], cache[0][c - 2]) + map[1][c];

c++ 코드

#include <iostream>
#include <cstring>
using namespace std;

#define MAX 100001
#define endl "\n"

int map[2][MAX];
int cache[2][MAX];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie();

	int testCase;
	cin >> testCase;

	int n;

	memset(map, 0, sizeof(map));

	for (int i = 0; i < testCase; ++i)
	{
		cin >> n;
		for (int j = 0; j < 2; ++j)
		{
			for (int k = 1; k <= n; ++k)
				cin >> map[j][k];
		}

		cache[0][0] = cache[1][0] = 0;
		cache[0][1] = map[0][1];
		cache[1][1] = map[1][1];
		
		for (int c = 2; c <= n; ++c)
		{
			cache[0][c] = max(cache[1][c-1], cache[1][c-2]) + map[0][c];
			cache[1][c] = max(cache[0][c - 1], cache[0][c - 2]) + map[1][c];
		}

		cout << max(cache[0][n], cache[1][n]) << endl;
		memset(map, 0, sizeof(map));
		memset(cache, 0, sizeof(cache));

	}

	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글