https://www.acmicpc.net/problem/9465
9465번: 스티커
첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 n (1 ≤ n ≤ 100,000)이 주어진다. 다음 두 줄에는 n개의 정수가 주어지며, 각 정수는 그 위치에 해당하는 스티커의
www.acmicpc.net
동물원 문제와 유사합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include <iostream>
#include <algorithm>
using namespace std;
long long stic[2][100004];
long long d[3][100004];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i <= 1; i++)
{
for (int j = 1; j <= n; j++)
cin >> stic[i][j];
}
d[0][1] = 0;
d[1][1] = stic[0][1];
d[2][1] = stic[1][1];
// 0 안고름 1 위 2 아래
for (int j = 2; j <= n; j++)
{
d[0][j] = max({ d[0][j - 1], d[1][j - 1] ,d[2][j - 1] });
d[1][j] = max({ d[0][j - 1], d[2][j - 1] }) + stic[0][j];
d[2][j] = max({ d[0][j - 1], d[1][j - 1] }) + stic[1][j];
}
long long ans = max({ d[0][n], d[1][n] ,d[2][n] });
cout << ans << '\n';
}
return 0;
}
|
cs |
'다이나믹 프로그래밍' 카테고리의 다른 글
정수 삼각형 (0) | 2023.03.28 |
---|---|
포도주 시식 (0) | 2023.03.28 |
RGB 거리 & 동물원 & 오르막수 (0) | 2023.03.27 |
합분해 (0) | 2023.03.27 |
제곱수의 합 (0) | 2023.03.27 |