https://www.acmicpc.net/problem/1912
1912번: 연속합
첫째 줄에 정수 n(1 ≤ n ≤ 100,000)이 주어지고 둘째 줄에는 n개의 정수로 이루어진 수열이 주어진다. 수는 -1,000보다 크거나 같고, 1,000보다 작거나 같은 정수이다.
www.acmicpc.net
d[i] = MAX (d[i-1] + a[i], a[i])
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
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100004];
int d[100004];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
d[0] = a[0];
for (int i = 1; i < n; i++)
{
d[i] = a[i];
if (d[i - 1] + a[i] > a[i])
{
d[i] = d[i - 1] + a[i];
}
}
int ans = d[0];
for (int i = 0; i < n; i++)
{
if (d[i] > ans)
ans = d[i];
}
cout << ans << '\n';
return 0;
}
|
cs |
'다이나믹 프로그래밍' 카테고리의 다른 글
합분해 (0) | 2023.03.27 |
---|---|
제곱수의 합 (0) | 2023.03.27 |
가장 긴 증가하는 부분 순열 (출력) (0) | 2023.03.26 |
가장 긴 증가하는 부분 수열 (0) | 2023.03.26 |
쉬운 계단 수 & 이친수 (0) | 2023.03.23 |