IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> C. Alternating Subsequence -> 正文阅读

[开发测试]C. Alternating Subsequence

C. Alternating Subsequence

C.交替子序列

time limit per test: 1 second
每次测试的时间限制:1秒

memory limit per test: 256 megabytes
每次测试的内存限制:256兆字节

input: standard input
输入:标准输入

output: standard output
产出:标准产出

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
回想一下,序列b是序列a的子序列,如果b可以通过移除零或多个元素而不从a派生。

changing the order of the remaining elements. For example, ifa = 1, 2,1,3,1,2, 1],then possible subsequences are: [1, 1,1,1], [3]
changing the order of the remaining elements. For example, ifa = 1, 2,1,3,1,2, 1],then possible subsequences are: [1, 1,1,1], [3]

and [1,2, 1,3,1, 2, 1]. butnot [3, 2, 3] and[1,1,1,1,2].
和[1,2,1,3,1,2,1]。但不是[3,2,3]和[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).
您将得到一个由n个正负元素组成的序列a(序列中没有零)。

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
换句话说,如果交替子序列的最大长度为k,那么您的任务就是找到一些元素的最大和。

alternating subsequence of length k.
长度k的交替子序列。

You have to answer t independent test cases.
您必须回答独立的测试用例。

Input
输入

The first line of the input contains one integert(1≤t≤10*)- - the number of test cases. Then t test cases follow.
The first line of the input contains one integert(1≤t≤10*)- - the number of test cases. Then t test cases follow.

The first line of the test case contains one integern(1≤n≤2. 10*)- the number of elements in a. The second line of the test case
测试用例的第一行包含一个整数(1≤n≤2.10*)–测试用例的第二行中的元素数。

contains n integers a1,a2…,an(-10’≤ai≤10’,ai≠0), where a; is the i-th element of a.
包含n个整数A1,a2.,an(-10‘≤ai≤10’,ai≠0),其中a;是a的第一个元素。

It is guaranteed that the sum of n over all test cases does not exceed2. 10°(2n≤2. 105).
It is guaranteed that the sum of n over all test cases does not exceed2. 10°(2n≤2. 105).

Output
输出量

For each test case, print the answer一the maximum sum of the maximum by size (length) alternating subsequence of a.
对于每个测试用例,请打印答案(一)–按大小(长度)计算的最大和。

oj上消灭星星的变种
按正负分段每段取最大值

ll a[200007];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        int cnt1=0,cnt2=0;
        ll max1=-1e9;
        for(int i=0;i<n;++i)
        {
            cin>>a[i];
            if(a[i]<0)
                cnt1++;
            else
                cnt2++;
            max1=max(max1,a[i]);
        }
        if(cnt1==n||cnt2==n)
        {
            cout<<max1<<endl;
            continue;
        }
        ll sum=0;

        //        int x=n-1;
        for(int i=0;i<n;)
        {

            bool flag=0;
            max1=-1e9;
            while(a[i]>0&&i<n)
                flag=1,max1=max(max1,a[i]),i++;
            if(flag)
                sum+=max1;
            flag=0,max1=-1e9;
            while(a[i]<0&&i<n)
                flag=1,max1=max(max1,a[i]),i++;
            if(flag)
                sum+=max1;
        }
        cout<<sum<<endl;
    }
    return 0;
}
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-27 12:10:10  更:2021-08-27 12:11:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 21:00:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码