Notice
Recent Posts
Recent Comments
Link
거의 알고리즘 일기장
백준 9465번 _ 스티커 본문
https://www.acmicpc.net/problem/9465
9465번: 스티커
문제 상근이의 여동생 상냥이는 문방구에서 스티커 2n개를 구매했다. 스티커는 그림 (a)와 같이 2행 n열로 배치되어 있다. 상냥이는 스티커를 이용해 책상을 꾸미려고 한다. 상냥이가 구매한 스티커의 품질은 매우 좋지 않다. 스티커 한 장을 떼면, 그 스티커와 변을 공유하는 스티커는 모두 찢어져서 사용할 수 없게 된다. 즉, 뗀 스티커의 왼쪽, 오른쪽, 위, 아래에 있는 스티커는 사용할 수 없게 된다. 모든 스티커를 붙일 수 없게된 상냥이는 각 스티커에 점
www.acmicpc.net
풀이방법
선택의 경우가 3가지가 있다.
위의 경우들을 끝까지 갈때까지 반복하면됨.
전체코드
#include <iostream>
#include <algorithm>
using namespace std;
int stikers[3][1000001];
int dp[3][1000001];
int n;
void dpInit()
{
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = -1;
}
void Input()
{
cin >> n;
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= n; j++)
cin >> stikers[i][j];
}
int getMaxValue(int status, int here)
{
int& ret = dp[status][here];
if (ret != -1) return ret;
ret = 0;
if (status != 0 && here == n)
return ret = stikers[status][here];
if (status == 0 && here == n)
return ret = max(stikers[1][here], stikers[2][here]);
//선택 no
ret = max(ret, getMaxValue(0, here + 1));
//선택 1
if(status == 1 || status == 0)
ret = max(ret, getMaxValue(2, here + 1) + stikers[1][here]);
//선택 2
if(status == 2 || status == 0)
ret = max(ret, getMaxValue(1, here + 1) + stikers[2][here]);
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int testcase;
cin >> testcase;
for (int i = 0; i < testcase; i++)
{
Input();
dpInit();
cout << getMaxValue(0, 1) << '\n';
}
return 0;
}
후기
쉬운 dp도 나한텐 어렵네..
반응형
'알고리즘 문제풀이' 카테고리의 다른 글
백준 1699번 _ 제곱수의 합 (0) | 2020.05.09 |
---|---|
백준 2294번 _ 동전 2 (0) | 2020.05.09 |
알고스팟 _ LIS, 백준 12015번 _가장 긴 증가하는 부분 수열 2 (0) | 2020.05.08 |
알고스팟 _ 삼각형 위의 최대 경로 (0) | 2020.05.08 |
codeforces _ 230B _ B. T-primes (0) | 2020.05.07 |
Comments