news 2026/2/28 13:23:15

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/2/26 8:03:52

完整教程:从零开始掌握SmolVLM2视觉语言模型实战技巧

完整教程&#xff1a;从零开始掌握SmolVLM2视觉语言模型实战技巧 【免费下载链接】smol-course A course on aligning smol models. 项目地址: https://gitcode.com/gh_mirrors/smo/smol-course 想要快速上手多模态AI应用&#xff1f;SmolVLM2视觉语言模型正是你需要的解…

作者头像 李华
网站建设 2026/3/1 0:15:30

菜单栏革命:用Reminders MenuBar重塑你的任务管理体验

你是否厌倦了在多个应用间来回切换&#xff0c;只为查看今天的待办事项&#xff1f;当重要提醒被埋没在系统通知中时&#xff0c;是否感到工作效率大打折扣&#xff1f;今天&#xff0c;让我们一起探索这款能够真正改变你工作方式的macOS应用——Reminders MenuBar。 【免费下载…

作者头像 李华
网站建设 2026/2/18 13:57:39

串口通信中奇偶校验实现原理

串口通信中的奇偶校验&#xff1a;从原理到实战的深度解析在嵌入式开发的世界里&#xff0c;UART&#xff08;通用异步收发器&#xff09;是最基础、也最常用的通信接口之一。无论是调试打印日志、连接传感器&#xff0c;还是与工业设备交互&#xff0c;我们几乎每天都在和串口…

作者头像 李华
网站建设 2026/3/1 2:23:53

5分钟精通Telegraf处理器:数据清洗的实战进阶指南

5分钟精通Telegraf处理器&#xff1a;数据清洗的实战进阶指南 【免费下载链接】telegraf 插件驱动的服务器代理&#xff0c;用于收集和报告指标。 项目地址: https://gitcode.com/GitHub_Trending/te/telegraf 在监控系统构建过程中&#xff0c;原始数据往往面临格式混乱…

作者头像 李华
网站建设 2026/2/24 18:34:53

仿宋GB2312字体安装终极完整指南:零基础快速上手

还在为文档排版不够专业而烦恼吗&#xff1f;仿宋GB2312字体安装其实比你想象的要简单很多。作为一名长期使用这款经典字体的文档工作者&#xff0c;今天我要分享我的实战心得&#xff0c;让你轻松掌握这款字体的完整安装流程。 【免费下载链接】仿宋GB2312字体安装指南分享 仿…

作者头像 李华
网站建设 2026/2/27 5:13:51

仓颉编程语言:从零开始的完整入门指南

仓颉编程语言&#xff1a;从零开始的完整入门指南 【免费下载链接】CangjieCommunity 为仓颉编程语言开发者打造活跃、开放、高质量的社区环境 项目地址: https://gitcode.com/Cangjie/CangjieCommunity 仓颉编程语言作为一款面向全场景智能应用开发的新型编程语言&…

作者头像 李华