zoukankan      html  css  js  c++  java
  • D. Maximum Sum on Even Positions

    You are given an array aa consisting of nn integers. Indices of the array start from zero (i. e. the first element is a0a0, the second one is a1a1, and so on).

    You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of aa with borders ll and rr is a[l;r]=al,al+1,,ara[l;r]=al,al+1,…,ar.

    Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements a0,a2,,a2ka0,a2,…,a2k for integer k=n12k=⌊n−12⌋ should be maximum possible).

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t21041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (1n21051≤n≤2⋅105) — the length of aa. The second line of the test case contains nn integers a0,a1,,an1a0,a1,…,an−1 (1ai1091≤ai≤109), where aiai is the ii-th element of aa.

    It is guaranteed that the sum of nn does not exceed 21052⋅105 (n2105∑n≤2⋅105).

    Output

    For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of aa.

    Example
    input
    Copy
    4
    8
    1 7 3 4 7 6 2 9
    5
    1 2 1 2 1
    10
    7 8 4 5 7 6 8 9 7 3
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int N = 3e3 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            int n;
            cin >> n;
            ll sum = 0;
            vector<ll> a(n),odd,even;
            forn(i, n)
            {
                cin >> a[i];
                if (i % 2)
                    odd.push_back(a[i]);
                else
                    even.push_back(a[i]),sum+=a[i];
            }
            int i = 0;
            ll res = 0,tmp=0;
            for (int i = 0; i < n / 2; i++)
            {
                tmp = max(0ll,tmp + odd[i] - even[i]);
                res = max(res, tmp);
            }
            tmp=0;
            int sz = n / 2;
            if (n % 2)
                sz++;
            for (int i = 0; i < sz-1; i++)
            {
                tmp = max(0ll, tmp + odd[i] - even[i+1]);
                res = max(res, tmp);
            }
            cout << sum + res << endl;
        }
        return 0;
    }
    4
    3 1 2 1
    
    output
    Copy
    26
    5
    37
    5
    
  • 相关阅读:
    html 复选框
    html 单选框
    C++创建类的对象(类的初始化)的方法
    C++创建类的对象(类的初始化)的方法
    C#基础实现URL Unicode编码,编码、解码相关整理
    C#基础实现URL Unicode编码,编码、解码相关整理
    js 发送http请求
    js 发送http请求
    C++的三种实例化对象方式
    C++的三种实例化对象方式
  • 原文地址:https://www.cnblogs.com/dealer/p/13204883.html
Copyright © 2011-2022 走看看