题目大意
给出两个
n
n
n维数组
a
1
,
a
2
,
⋯
,
a
n
a_1,a_2,\cdots,a_n
a1,a2,⋯,an和
b
1
,
b
2
,
⋯
,
b
n
b_1,b_2,\cdots,b_n
b1,b2,⋯,bn。
在
a
,
b
a,b
a,b两个数组中,交替选择且每次选择的数的下标都要严格大于上一次选择的数的下标。
求选出数的所有数的总和最大是多少。
时间限制
2s
数据范围
n
≤
1
0
5
n\le10^5
n≤105
a
i
,
b
i
≤
1
0
9
a_i,b_i\le10^9
ai,bi≤109
题解
一个简单的DP,
设
f
i
f_i
fi表示前
i
i
i个数,且最后一个数是在
a
a
a中选择的最大值,
设
g
i
g_i
gi表示前
i
i
i个数,且最后一个数是在
b
b
b中选择的最大值,
转移就很,
f
i
=
max
{
f
i
−
1
,
g
i
−
1
+
a
i
}
f_i=\max\{f_{i-1} , g_{i-1}+a_i\}
fi=max{fi−1,gi−1+ai}
g
i
=
max
{
g
i
−
1
,
f
i
−
1
+
b
i
}
g_i=\max\{g_{i-1} , f_{i-1}+b_i\}
gi=max{gi−1,fi−1+bi}
Code
//#pragma GCC optimize (2)
//#pragma G++ optimize (2)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#define G getchar
#define ll long long
using namespace std;
int read()
{
char ch;
for(ch = G();(ch < '0' || ch > '9') && ch != '-';ch = G());
int n = 0 , w;
if (ch == '-')
{
w = -1;
ch = G();
} else w = 1;
for(;'0' <= ch && ch <= '9';ch = G())n = (n<<1)+(n<<3)+ch-48;
return n * w;
}
const int N = 100005;
int n , a[N] , b[N];
ll g , f , f1 , g1;
int main()
{
//freopen("d.in","r",stdin);
//freopen("d.out","w",stdout);
n = read();
for (int i = 1 ; i <= n ; i ++)
a[i] = read();
for (int i = 1 ; i <= n ; i ++)
b[i] = read();
f = g = 0;
for (int i = 1 ; i <= n; i++)
{
f1 = max(f , g + a[i]);
g1 = max(g , f + b[i]);
f = f1;
g = g1;
}
printf("%lld\n", max(f , g));
return 0;
}