Long Long

Long Long

题面翻译

题目描述
给出一个包含 n 个数字的数列 a。你可以执行任意次操作,每次操作可以更改 [l, r] 范围内的正负性(正数变负,负数变正,0 不变)。你要使得数列每个元素之和尽量大,问最小的操作次数。

多组询问。

 输入格式
第一行一个整数 T,表示询问组数。

每一组数据第一行输入一个整数 n,表述数列长度。

第二行 n 个整数 a1,a2,...,an 表示数列 a 中的每个元素。

输出格式
对于每组数据,每一行,输出两个用空格隔开的整数,分别表示最大可能的数列元素之和与最小操作次数。

数据范围
1T104

1n2×105

109ai109

数据保证所有询问的 n 总和不超过 2×105

提示
统计数字之和部分可能会爆 int,请选择合适的储存方式。

题目描述

Today Alex was brought array a1,a2,,an of length n . He can apply as many operations as he wants (including zero operations) to change the array elements.

In 1 operation Alex can choose any l and r such that 1lrn , and multiply all elements of the array from l to r inclusive by 1 . In other words, Alex can replace the subarray [al,al+1,,ar] by [al,al+1,,ar] in 1 operation.

For example, let n=5 , the array is [1,2,0,3,1] , l=2 and r=4 , then after the operation the array will be [1,2,0,3,1] .

Alex is late for school, so you should help him find the maximum possible sum of numbers in the array, which can be obtained by making any number of operations, as well as the minimum number of operations that must be done for this.

输入格式

The first line contains a single integer t ( 1t104 ) — number of test cases. Then the descriptions of the test cases follow.

The first line of each test case contains one integer n ( 1n2105 ) — length of the array.

The second line contains n integers a1,a2,,an ( 109ai109 ) — elements of the array.

It is guaranteed that the sum of n for all test cases does not exceed 2105 .

输出格式

For each test case output two space-separated numbers: the maximum possible sum of numbers in the array and the minimum number of operations to get this sum.

Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type.

## 样例 #1

### 样例输入 #1

```
5
6
-1 7 -4 -2 5 -8
8
-1 0 0 -2 1 0 -3 0
5
2 -1 0 -3 -7
5
0 -17 0 1 0
4
-1 0 -2 -1
```

### 样例输出 #1

```
27 3
7 2
13 1
18 1
4 1
```

## 提示

Below, for each test case, only one of the possible shortest sequences of operations is provided among many. There are others that have the same length and lead to the maximum sum of elements.

In the first test case, Alex can make operations: (1,4) , (2,2) , (6,6) .

In the second test case, to get the largest sum you need to make operations: (1,8) , (5,6) .

In the fourth test case, it is necessary to make only one operation: (2,3) .

复制代码
// //Long Long #include <bits/stdc++.h> #define int long long using namespace std; const int N=1e6+10,mod=1e9+7; string s; int n,t,a[N],f[N],res,num,ans,m; bool vis[N]; signed main() { std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n; res=0,ans=0; bool f=false; for(int i=0;i<n;i++){ cin>>m; ans+=abs(m); if(m<0&&!f) res++,f=true; else if(m==0&&f) continue; else if(m>0&&f) f=false; } cout<<ans<<" "<<res<<endl; } return 0; }
复制代码

 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17544874.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示