[BOJ][1912] 연속합
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/1912
2. 사용 알고리즘
DP
3. 풀이
d[n] : a[n]을 마지막으로 하는 최대 연속합
-
n - 1 번째 수를 포함하는 경우 : d[n - 1] + a[n]
-
n - 1 번째 수를 포함하지 않는 경우 : a[n]
∴ d[n] = max(d[n - 1] + a[n], a[n])
∴ max(d[i]) (1 <= i <= n)
4. 소스 코드
4-1. C++
4-1-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/1912.cpp
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
#include <iostream>
#include <algorithm>
using namespace std;
int a[100003], d[100003];
int solve(int n) {
if (d[n] != -2147483648) return d[n];
return d[n] = max(solve(n - 1) + a[n], a[n]);
}
int main(void) {
ios_base::sync_with_stdio(false);
int n; cin >> n;
int ans;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1) {
ans = d[1] = a[1];
continue;
}
d[i] = -2147483648;
solve(i);
if (ans < d[i]) ans = d[i];
}
cout << ans << "\n";
return 0;
}
4-1-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/1912_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;
int a[100003], d[100003];
int main(void) {
ios_base::sync_with_stdio(false);
int n; cin >> n;
int ans;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1) {
ans = d[1] = a[1];
continue;
}
d[i] = max(d[i - 1] + a[i], a[i]);
if (ans < d[i]) ans = d[i];
}
cout << ans << "\n";
return 0;
}
4-2. JAVA
4-2-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/1912.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.max;
public class Main {
static int a[] = new int[100003];
static int d[] = new int[100003];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int ans = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 1; i <= n; ++i) {
a[i] = Integer.parseInt(st.nextToken());
if (i == 1) {
ans = d[1] = a[1];
continue;
}
d[i] = -2147483648;
solve(i);
if (ans < d[i]) ans = d[i];
}
System.out.println(ans);
}
public static int solve(int n) {
if (d[n] != -2147483648) return d[n];
return d[n] = max(solve(n - 1) + a[n], a[n]);
}
}
4-2-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/1912_2.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.max;
public class Main {
static int a[] = new int[100003];
static int d[] = new int[100003];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int ans = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 1; i <= n; ++i) {
a[i] = Integer.parseInt(st.nextToken());
if (i == 1) {
ans = d[1] = a[1];
continue;
}
d[i] = max(d[i - 1] + a[i], a[i]);
if (ans < d[i]) ans = d[i];
}
System.out.println(ans);
}
}
Leave a comment