当前位置: 首页 > news >正文

B. Long Long

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

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

For example, let n=5�=5, the array is [1,−2,0,3,−1][1,−2,0,3,−1], l=2�=2 and r=4�=4, then after the operation the array will be [1,2,0,−3,−1][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.

Input

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

The first line of each test case contains one integer n� (1≤n≤2⋅1051≤�≤2⋅105) — length of the array.

The second line contains n� integers a1,a2,…,an�1,�2,…,�� (−109≤ai≤109−109≤��≤109) — elements of the array.

It is guaranteed that the sum of n� for all test cases does not exceed 2⋅1052⋅105.

Output

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.

Example

input

Copy

 

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

output

Copy

27 3
7 2
13 1
18 1
4 1

Note

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)(1,4), (2,2)(2,2), (6,6)(6,6).

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

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

解题说明:此题是一道数学题,采用贪心算法,最大值肯定是所有数取绝对值相加,至于需要转换的次数,直接遍历查找其中的负数,如果负数单独出现就只转换一个负数,否则转换连续出现的负数。

#include <stdio.h>
#include <stdlib.h>
typedef long long LL;
int a[200010];int main()
{int t = 0;scanf("%d", &t);while (t--){int n = 0;scanf("%d", &n);LL cnt = 0, sum = 0;for (int i = 0; i < n; i++){scanf("%d", &a[i]);if (a[i] > 0){sum += a[i];}else{sum -= a[i];}}for (int i = 0, j = 0; i < n; i++){if (a[i] >= 0){continue;}j = i;while (a[j] <= 0){j++;}if (j != i){cnt++;}i = j - 1;}printf("%lld %lld\n", sum, cnt);}return 0;
}

http://www.lryc.cn/news/153013.html

相关文章:

  • CTFhub-文件上传-.htaccess
  • Python中的绝对和相对导入
  • C语言关于与运算符
  • 计算机网络(速率、宽带、吞吐量、时延、发送时延)
  • kubectl入门
  • Android JNI系列详解之ndk-build工具的使用
  • 【业务功能篇90】微服务-springcloud-检索服务-ElasticSearch实战运用-DSL语句
  • QTday4
  • 设计模式之命令模式(Command)的C++实现
  • 取证工具prodiscover的基本操作
  • flutter plugins插件【二】【FlutterAssetsGenerator】
  • 看懂UML类图
  • keras深度学习框架通过简单神经网络实现手写数字识别
  • React 中的 ref 如何操作 dom节点,使输入框获取焦点
  • 最短路Dijkstra,spfa,图论二分图算法AYIT---ACM训练(模板版)
  • AK 微众银行 9.3 笔试 Java后端方向
  • 了解java中的通配符“?“
  • 浙大陈越何钦铭数据结构07-图6 旅游规划【最小堆实现】
  • OpenShift 4 - 用 Prometheus 和 Grafana 监视用户应用定制的观测指标(视频)
  • 【LeetCode】剑指 Offer <二刷>(3)
  • Ceph IO流程及数据分布
  • Netty-NIO
  • 红外物理学习笔记 ——第三章
  • 使用 htmx 构建交互式 Web 应用
  • S32K324芯片学习笔记
  • htmx-使HTML更强大
  • Java学习之序列化
  • C++实现蜂群涌现效果(flocking)
  • IDEA复制一个工程为多个并启动,测试负载均衡
  • 001_C++语法基础