File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
Dynamic_programming/problem solving Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #define MAX 100001
3
+ #define MAX_VAL 900001
4
+ #define MIN_VAL -1
5
+ using namespace std ;
6
+
7
+ int n;
8
+ int dp_max[2 ][3 ];
9
+ int dp_min[2 ][3 ];
10
+
11
+ bool inRange (int x) {
12
+ return (0 <= x && x <= 2 );
13
+ }
14
+
15
+ int main () {
16
+ ios::sync_with_stdio (false );
17
+ cin.tie (NULL );
18
+ cout.tie (NULL );
19
+
20
+ cin >> n;
21
+
22
+ // memory 초과 문제를 줄여보자.
23
+ for (int i = 0 ; i < n; i++) {
24
+ int x, y, z;
25
+ cin >> x >> y >> z;
26
+
27
+ dp_max[1 ][0 ] = (dp_max[0 ][0 ] + x > dp_max[0 ][1 ] + x) ? dp_max[0 ][0 ] + x : dp_max[0 ][1 ] + x;
28
+
29
+ dp_max[1 ][1 ] = (dp_max[0 ][0 ] + y > dp_max[0 ][1 ] + y) ? dp_max[0 ][0 ] + y : dp_max[0 ][1 ] + y;
30
+ dp_max[1 ][1 ] = (dp_max[1 ][1 ] > dp_max[0 ][2 ] + y) ? dp_max[1 ][1 ] : dp_max[0 ][2 ] + y;
31
+
32
+ dp_max[1 ][2 ] = (dp_max[0 ][1 ] + z > dp_max[0 ][2 ] + z) ? dp_max[0 ][1 ] + z : dp_max[0 ][2 ] + z;
33
+
34
+
35
+ dp_min[1 ][0 ] = (dp_min[0 ][0 ] + x < dp_min[0 ][1 ] + x) ? dp_min[0 ][0 ] + x : dp_min[0 ][1 ] + x;
36
+
37
+ dp_min[1 ][1 ] = (dp_min[0 ][0 ] + y < dp_min[0 ][1 ] + y) ? dp_min[0 ][0 ] + y : dp_min[0 ][1 ] + y;
38
+ dp_min[1 ][1 ] = (dp_min[1 ][1 ] < dp_min[0 ][2 ] + y) ? dp_min[1 ][1 ] : dp_min[0 ][2 ] + y;
39
+
40
+ dp_min[1 ][2 ] = (dp_min[0 ][1 ] + z < dp_min[0 ][2 ] + z) ? dp_min[0 ][1 ] + z : dp_min[0 ][2 ] + z;
41
+
42
+
43
+ // 한 칸 내린다.
44
+ for (int j = 0 ; j < 3 ; j++) {
45
+ dp_max[0 ][j] = dp_max[1 ][j];
46
+ dp_min[0 ][j] = dp_min[1 ][j];
47
+ }
48
+ }
49
+
50
+ int max_res = MIN_VAL, min_res = MAX_VAL;
51
+ for (int j = 0 ; j < 3 ; j++) {
52
+ max_res = (max_res > dp_max[1 ][j]) ? max_res : dp_max[1 ][j];
53
+ min_res = (min_res < dp_min[1 ][j]) ? min_res : dp_min[1 ][j];
54
+ }
55
+
56
+ cout << max_res << " " << min_res;
57
+
58
+ return 0 ;
59
+ }
You can’t perform that action at this time.
0 commit comments