zoukankan      html  css  js  c++  java
  • Alternating Subsequence CodeForces

    Recall that the sequence bb is a a subsequence of the sequence aa if bb can be derived from aa by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1]a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1][1,1,1,1], [3][3] and [1,2,1,3,1,2,1][1,2,1,3,1,2,1], but not [3,2,3][3,2,3] and [1,1,1,1,2][1,1,1,1,2].

    You are given a sequence aa consisting of nn positive and negative elements (there is no zeros in the sequence).

    Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

    In other words, if the maximum length of alternating subsequence is kk then your task is to find the maximum sum of elements of some alternating subsequence of length kk.

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t1041≤t≤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 number of elements in aa. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (109ai109,ai0−109≤ai≤109,ai≠0), where aiai is the ii-th element of aa.

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

    Output

    For each test case, print the answer — the maximum sum of the maximum by size (length) alternatingsubsequence of aa.

    Example

    Input
    4
    5
    1 2 3 -1 -2
    4
    -1 -2 -1 -3
    10
    -2 8 3 8 -4 -15 5 -2 -3 1
    6
    1 -1000000000 1 -1000000000 1 -1000000000
    
    Output
    2
    -1
    6
    -2999999997
    按正负分割数组,取每个同号block中的最大值最后相加
    auto让代码优雅许多。。
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll inf = 1<<30;
    const int mod = 1000000007;
    const int mx = 1e4+1e2; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, -1, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pai
    //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int n, m, k,ans,t;
    
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);    
        cin >> t;
        auto sgn = [&](int x) {
            if (x>0)return 1;
            else return -1;
        };
        while (t--) {
            cin >> n; ll sum = 0;
            vector<int>a(n);
            for (auto &it : a)cin >> it;
            re(i, 0, n) {
                int cur = a[i];
                int j = i;
                while (j<n&&sgn(a[i])==sgn(a[j]))
                {
                    cur = max(cur, a[j]);
                    ++j;
                }
                sum += cur;
                i = j - 1;
            }
            cout << sum << endl;
        }
        return 0;
    }
  • 相关阅读:
    C#对象初始化器
    C#构造方法
    C#方法重载
    C#方法
    C#类 对象 字段和属性
    C#数组
    C#字符串
    C#原码反码补码
    字段、方法、属性
    单例模式、异常
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12762246.html
Copyright © 2011-2022 走看看