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

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

cpp 复制代码
#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;
}
相关推荐
Wenweno0o15 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
小O的算法实验室15 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
chenjingming66615 小时前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
qq_3395548215 小时前
英飞凌ModusToolbox环境搭建
c语言·eclipse
cch891815 小时前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳15 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发15 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense15 小时前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎16 小时前
STL 栈 队列
开发语言·c++
勿忘,瞬间16 小时前
数据结构—顺序表
java·开发语言