Codeforces 1469 B. Red and Blue

Codeforces 1469 B. Red and Blue
在这里插入图片描述
在这里插入图片描述
题意分析:
第一遍读题的时候,没看清题,其实给定的 r 和 b 序列的顺序是不能更改的,因此 wa 了一发
第二遍读题发现,其实就是分开求 r 和 b 序列的最大前缀和,然后让两个再相加即可

AC代码:

#include <iostream>
#define ll long long
using namespace std;

const int N = 110;

int a[N],b[N];

int main() {
    
    
    int t;
    int n,m;
    cin >> t;
    while (t--) {
    
    
        cin >> n;
        ll sum = 0;
        ll cur = 0;
        for(int i = 0;i < n; i++) {
    
    
            cin >> a[i];
            cur += a[i];
            sum = max(sum ,cur);
        }
        ll ans = sum;
        sum = 0;
        cur = 0;
        cin >> m;
        for(int i = 0;i < m; i++) {
    
    
            cin >> b[i];
            cur += b[i];
            sum = max(sum ,cur);
        }
        cout << sum + ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45654671/article/details/112783257
今日推荐