zoukankan      html  css  js  c++  java
  • Codeforces Round #636 C. Alternating Subsequence

    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) alternating subsequence of aa .

    Example
    Input
    Copy
    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
    Copy
    2
    -1
    6
    -2999999997

    猛一看似乎有点难,再一看发现就是把连续的同号的数看成一个块,把块内最大值累加到答案就行了,边读入边处理,数组都不用开0.0
    #include <bits/stdc++.h>
    using namespace std;
    long long n,a[200005];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n;
            long long i,pre,tempmax=-0x3f3f3f3f,ans=0;
            for(i=1;i<=n;i++)
            {
                scanf("%lld",&a[i]);
                if(i==1)
                {
                    if(a[i]>0)pre=1;
                    else pre=-1;
                    tempmax=a[1]; 
                }
                else
                {
                    if(a[i]*pre<0)//换了块 
                    {
                        pre=-pre;
                        ans+=tempmax;
                        tempmax=a[i];
                    }
                    else
                    {
                        tempmax=max(tempmax,a[i]);
                    }
                }
            }
            ans+=tempmax;//最后一个块 
            cout<<ans<<endl;
        }
        return 0;
    }


  • 相关阅读:
    20、Python之函数参数的使用
    19、Python之函数的基本使用
    18、Python之文件修改及f.seek的使用
    17、Python之文件处理的其他方法
    15、字符编码
    14、Python基本数据类型及内置方法(集合)
    13、Python基本数据类型及内置方法(列表、元组、字典)
    12、Python基本数据类型及内置方法(数字、字符串)
    11、Python流程控制之for循环
    10、Python流程控制之while循环
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12750213.html
Copyright © 2011-2022 走看看