C. Alternating Subsequence

C. Alternating Subsequence

          time limit per test:1 second
         memory limit per test:256 megabytes
         input:standard input
          output:standard output

Recall that the sequence bbaabbaaa=[1,2,1,3,1,2,1]a=[1,2,1,3,1,2,1][1,1,1,1][1,1,1,1][3][3][1,2,1,3,1,2,1][1,2,1,3,1,2,1][3,2,3][3,2,3][1,1,1,1,2][1,1,1,1,2]You are given a sequence aannYour 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 kkkkYou have to answer ttInputThe first line of the input contains one integer tt1≤t≤1041≤t≤104ttThe first line of the test case contains one integer nn1≤n≤2⋅1051≤n≤2⋅105aanna1,a2,…,ana1,a2,…,an−109≤ai≤109,ai≠0−109≤ai≤109,ai≠0aiaiiiaa.It is guaranteed that the sum of nn2⋅1052⋅105∑n≤2⋅105∑n≤2⋅105OutputFor each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of aaExampleinputCopy4
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
outputCopy2
-1
6
-2999999997
NoteIn the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2][1,2,3_,−1_,−2]In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3][−1,−2,−1_,−3]In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–][−2_,8,3,8_,−4_,−15,5_,−2_,−3,1_]In the fourth test case of the example, one of the possible answers is [1–,−1000000000–––––––––––––,1–,−1000000000–––––––––––––,1–,−1000000000–––––––––––––][1_,−1000000000_,1_,−1000000000_,1_,−1000000000_]

源代码:

// A code block
var foo = 'bar';
// ```
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include <algorithm>
using namespace std;
long long a[200000]={0};
int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     long long int n,sum=0,i,max,j;
     cin>>n;
     long long int a[n];
     for(i=0;i<n;i++)
     scanf("%lld ",&a[i]);
     for(i=0;i<n;)
     {
         max=a[i];
         for(j=i+1;j<n;j++)
         {
             if(a[i]*a[j]>0)
             {
                 if(a[j]>max)
                 max=a[j];
             }
             else
             {
             i=j;     
             break;
             }
         }
         sum+=max;
          if(j==n)
         break;
     }
     cout<<sum<<endl;
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值