news 2026/4/23 8:29:46

C. Contrast Value

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C. Contrast Value

time limit per test

2 seconds

memory limit per test

256 megabytes

For an array of integers [a1,a2,…,an], let's call the value |a1−a2|+|a2−a3|+⋯+|an−1−an| the contrast of the array. Note that the contrast of an array of size 1 is equal to 0.

You are given an array of integers a. Your task is to build an array of b in such a way that all the following conditions are met:

  • b is not empty, i.e there is at least one element;
  • b is a subsequence of a, i.e b can be produced by deleting some elements from a (maybe zero);
  • the contrast of b is equal to the contrast of a.

What is the minimum possible size of the array b?

Input

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

The first line of each test case contains a single integer n (1≤n≤3⋅105) — the size of the array a.

The second line contains n integers a1,a2,⋅,an (0≤ai≤109) — elements of the array itself.

The sum of n over all test cases doesn't exceed 3⋅105.

Output

For each test case, print a single integer — the minimum possible size of the array b.

Example

Input

Copy

4

5

1 3 3 3 7

2

4 2

4

1 1 1 1

7

5 4 2 1 0 0 4

Output

Copy

2 2 1 3

解题说明:此题是一道数学题,找规律可以发现数组元素分布最高点和最低点到两端的端点的绝对值之差与整个段的绝对值之差相等,因此只需要统计最低点和最高点的数目就行了。

#include <stdio.h> int a[300005]; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int ans = 1; for (int i = 2, op = 0; i <= n; i++) { if (a[i] > a[i - 1] && op != 1) { ans++; op = 1; } else if (a[i] < a[i - 1] && op != -1) { ans++; op = -1; } } printf("%d\n", ans); } return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/22 20:06:06

通义千问Qwen:重塑开发效率的革命性编程助手

通义千问Qwen&#xff1a;重塑开发效率的革命性编程助手 【免费下载链接】Qwen The official repo of Qwen (通义千问) chat & pretrained large language model proposed by Alibaba Cloud. 项目地址: https://gitcode.com/GitHub_Trending/qw/Qwen 在当今快节奏的…

作者头像 李华
网站建设 2026/4/19 4:55:17

Docker流媒体服务器:5分钟搭建专业级RTMP直播推流平台

Docker流媒体服务器&#xff1a;5分钟搭建专业级RTMP直播推流平台 【免费下载链接】nginx-rtmp-docker Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming. 项目地址: https://gitcode.com/gh_mirrors/ng/nginx-rtmp-docke…

作者头像 李华
网站建设 2026/4/15 20:34:40

零配置智能视觉SLAM:5分钟开启革命性实时定位体验

零配置智能视觉SLAM&#xff1a;5分钟开启革命性实时定位体验 【免费下载链接】stella_vslam 项目地址: https://gitcode.com/gh_mirrors/ste/stella_vslam 你是否曾想过&#xff0c;如何让机器像人类一样"看懂"周围环境并实时定位自己&#xff1f;&#x1f…

作者头像 李华
网站建设 2026/4/22 23:47:29

Xilinx Zynq中VDMA双缓冲机制工作原理解析

深入理解Zynq中的VDMA双缓冲机制&#xff1a;从原理到实战在嵌入式视觉系统中&#xff0c;如何高效、稳定地传输图像数据&#xff0c;是每一个开发者都会面临的挑战。尤其是在Xilinx Zynq这样的异构SoC平台上&#xff0c;PS&#xff08;ARM处理器&#xff09;和PL&#xff08;F…

作者头像 李华
网站建设 2026/4/19 10:37:13

FREE!ship Plus 船舶设计终极指南:从零基础到专业应用

FREE!ship Plus 船舶设计终极指南&#xff1a;从零基础到专业应用 【免费下载链接】freeship-plus-in-lazarus FreeShip Plus in Lazarus 项目地址: https://gitcode.com/gh_mirrors/fr/freeship-plus-in-lazarus 还在为昂贵的船舶设计软件发愁吗&#xff1f;想要找到一…

作者头像 李华
网站建设 2026/4/18 21:53:20

利用PyTorch-CUDA-v2.6镜像在云服务器部署大模型训练任务

利用PyTorch-CUDA-v2.6镜像在云服务器部署大模型训练任务 当一个AI团队需要在48小时内完成从零搭建到启动百亿参数模型的训练任务时&#xff0c;传统环境配置方式几乎不可能实现。而今天&#xff0c;在主流云平台上选择“PyTorch-CUDA-v2.6”镜像创建GPU实例后&#xff0c;只需…

作者头像 李华