news 2026/4/16 16:57:52

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/16 11:59:12

3大核心优势:ABCJS让网页音乐渲染变得如此简单

3大核心优势&#xff1a;ABCJS让网页音乐渲染变得如此简单 【免费下载链接】abcjs javascript for rendering abc music notation 项目地址: https://gitcode.com/gh_mirrors/ab/abcjs ABCJS是一个强大的JavaScript库&#xff0c;能够将简单的ABC文本格式转换为精美的标…

作者头像 李华
网站建设 2026/4/16 13:43:57

Vortex模组管理器实战指南:从困惑到精通的解决方案

Vortex模组管理器实战指南&#xff1a;从困惑到精通的解决方案 【免费下载链接】Vortex Vortex: Nexus-Mods开发的游戏模组管理器&#xff0c;用于简化模组的安装和管理过程。 项目地址: https://gitcode.com/gh_mirrors/vor/Vortex 你是否曾经面对成堆的模组文件感到无…

作者头像 李华
网站建设 2026/4/16 13:43:13

MONAI医学AI框架快速上手:从零开始的完整指南

MONAI医学AI框架快速上手&#xff1a;从零开始的完整指南 【免费下载链接】MONAI AI Toolkit for Healthcare Imaging 项目地址: https://gitcode.com/GitHub_Trending/mo/MONAI 框架核心价值解析 MONAI&#xff08;Medical Open Network for AI&#xff09;是一个专门…

作者头像 李华
网站建设 2026/4/16 6:09:46

BERT-Large模型实战部署全攻略:从零开始到高效应用

BERT-Large模型实战部署全攻略&#xff1a;从零开始到高效应用 【免费下载链接】bert-large-uncased 项目地址: https://ai.gitcode.com/hf_mirrors/google-bert/bert-large-uncased 让我们一起来探索如何在本地环境中快速部署和应用BERT-Large模型。无论你是AI初学者还…

作者头像 李华
网站建设 2026/4/16 14:14:08

虚拟机性能测试新突破:VSOCK直连方案实战

虚拟机性能测试新突破&#xff1a;VSOCK直连方案实战 【免费下载链接】oha Ohayou(おはよう), HTTP load generator, inspired by rakyll/hey with tui animation. 项目地址: https://gitcode.com/gh_mirrors/oh/oha 你是否曾经在虚拟机环境进行性能测试时&#xff0c;感…

作者头像 李华
网站建设 2026/4/16 13:29:48

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 在当今快速迭代的…

作者头像 李华