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;
}
相关推荐
Dlrb12118 小时前
C语言-指针三
c语言·算法·指针·const·命令行参数
kkeeper~8 小时前
0基础C语言积跬步之深入理解指针(5下)
c语言·开发语言
Tisfy8 小时前
LeetCode 2540.最小公共值:双指针(O(m+n))
算法·leetcode·题解·双指针
一直不明飞行8 小时前
Java的equals(),hashCode()应该在什么时候重写
java·开发语言·jvm
IronMurphy8 小时前
【算法四十七】152. 乘积最大子数组
算法
盲敲代码的阿豪9 小时前
Python 入门基础教程(爬虫前置版)
开发语言·爬虫·python
basketball6169 小时前
C++ 构造函数完全指南:从入门到进阶
java·开发语言·c++
互联科技报9 小时前
2026超融合选型:Top5品牌与市场格局解读
开发语言·perl
weixin1997010801610 小时前
[特殊字符] 智能数据采集:数字化转型的“数据石油勘探队”(附Python实战源码)
开发语言·python
淘矿人10 小时前
Claude辅助DevOps实践
java·大数据·运维·人工智能·算法·bug·devops