Codeforce1343C. Alternating Subsequence
Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a 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], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].
You are given a sequence a consisting of n 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 k then your task is to find the maximum sum of elements of some alternating subsequence of length k.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).
Output
For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.
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
Note
In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].
In the second test case of the example, one of the possible answers is [−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–].
In the fourth test case of the example, one of the possible answers is [1–,−1000000000,1,-1000000000,1,−1000000000].
题目大意:
找到最长的+ - + - + - + - + -(+ -交替即可)的序列,求最长序列的最大值。
这道题好像没什么好说的,就是找出每一段全正或全负的子序列最大值作为最长序列的数即可,可以用双指针算法。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, a[N];
int main() {
//freopen("in.txt", "r", stdin);
int t; cin >> t;
while (t--) {
cin >> n;
ll ans = 0, now = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1)
now = a[i];
else {
if (a[i] * a[i - 1] > 0)
now = max(now, a[i]);
else
ans += now, now = a[i];
}
}
ans += now;//别忘记加上最后一位
cout << ans << endl;
}
}
Codeforce1343C. Alternating Subsequence的更多相关文章
- [CC-MCO16306]Fluffy and Alternating Subsequence
[CC-MCO16306]Fluffy and Alternating Subsequence 题目大意: 给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\). 对于一个 ...
- CF# 334 Alternative Thinking
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心
C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...
- Alternative Thinking 找规律
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
- CodeForces - 603A-Alternative Thinking (思维题)
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF #636 (Div. 3) 对应题号CF1343
unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...
- Codeforces Round #636 (Div. 3)
比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...
- codeforc 603-A. Alternative Thinking(规律)
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes Kevin ha ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
随机推荐
- HBuilderX代码提示失效解决方案
用了一段时间后,HBuilderX的代码提示功能突然不生效了,重新下载也没有用,解决方案是重置默认配置 提示:重置配置会清除编辑器内的项目,记得先保存 解决方案 1.找到HBuilderX根目录下的r ...
- Llinux登录后出现-bash-4.2#,解决办法以及造成这样的原因
版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin 1.原因是root在/root下面的几个配置文件丢失,丢失文件如下: 1..bash_profile 2..bashr ...
- Stable Diffusion扩散模型
人像生成模型 1.模型理论基础 扩散模型(Diffusion Model): 1.1 Diffusion Model 原理 首先,Denoise Model 需要一个起始的噪声图像作为输入.这个噪声图 ...
- [CF1854C] Expected Destruction
题目描述 You have a set $ S $ of $ n $ distinct integers between $ 1 $ and $ m $ . Each second you do th ...
- isAlive
线程存活 当线程执行时显示线程存活 执行完毕为false
- CH395实现主动ping对端功能(代码及说明)
目录 1.PING原理 1.1简介 1.2协议 1.3通信流程 2.代码解释 3.工程链接 PING原理 1.简介 PING是基于ICMP(Internet Control Message Proto ...
- 在WInform开发中实现工具栏/菜单的动态呈现
在Winform系统开发中,为了对系统的工具栏/菜单进行动态的控制,我们对系统的工具栏/菜单进行动态配置,这样可以把系统的功能弹性发挥到极致.通过动态工具栏/菜单的配置方式,我们可以很容易的为系统新增 ...
- 如何从零开始实现TDOA技术的 UWB 精确定位系统(1)
前言 这是一个系列文章,将向你介绍如何从零开始实现一个使用TDOA技术的 UWB 精确定位系统. 重要提示(劝退说明): Q:做这个定位系统需要基础么? A:文章不是写给小白看的,需要有电子技术和软件 ...
- BFS(三)单词接龙 ⅱ
对应 126. 单词接龙 II 问题描述 按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> ...
- 原生JavaScript 与 jQuery 执行Ajax请求
原生JavaScript和jQuery都可以用来执行Ajax请求,以下是它们的基本实现方式的比较: 原生JavaScript实现Ajax请求: var xhr = new XMLHttpRequest ...