Codeforce 1343C. 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.
输入:
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.
The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109,ai≠0−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 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
输出:
For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of aa.
Example
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
2 -1 6 -2999999997
提示:
In 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_].
题意:有T组数据,每组数据,第一行有一个整数N,第二行是N个数,从左到右,按一正一负或者一负一正的顺序,找到一个子序列,保证子序列最长的前提下,子序列的和最大,输出最大的和。
思路:先用一个数组b将数组a的数的正负记录下来,然后在将a[0]当作初值赋值maxx,在相同符号的区间内比较大小(正数最大,负数最大,它们的和最大),如果符号变化,就用sum把符号变化前最大的值加和,注意,数的最后没有符号变化,但要加上该区间的最大值,BUG人就错在这里了。
详细代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
typedef long long ll;
int i, j, k;
using namespace std;
int a[222222], b[222222];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] < 0)
b[i] = -1;
else
b[i] = 1;
}
int maxx = a[0];
ll sum = 0;
for (i = 1; i < n; i++)
{
if (b[i] == b[i - 1])
{
if (a[i] > maxx)
maxx = a[i];
}
else
{
sum += maxx;
maxx = a[i];
}
}
cout << sum + maxx << endl;
}
return 0;
}
第二篇博客,主要是刷题刷累了,早八晚九,写个博客玩玩,放松放松嘿嘿。
博主大二蒟蒻一枚,今天codeforce刷题量才过200,喜欢做codeforce的,就点个关注吧~

浙公网安备 33010602011771号