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;
}
相关推荐
natide2 小时前
表示/嵌入差异-1-欧几里得距离(Euclidean Distance)-L2 距离(L2 distance)-欧式距离的标准化
人工智能·pytorch·python·深度学习·算法·自然语言处理
雪花desu2 小时前
【Hot100-Java简单】:两数之和 (Two Sum) —— 从暴力枚举到哈希表的思维跃迁
java·数据结构·算法·leetcode·哈希表
qzhqbb2 小时前
群智能计算核心算法全解析
人工智能·算法
leaves falling2 小时前
c语言打印闰年
java·c语言·算法
YGGP2 小时前
【Golang】LeetCode 121. 买卖股票的最佳时机
算法·leetcode
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 37--测试报告 Allure 前置步骤-配置安装 JDK 详细图文教程
java·开发语言·学习·测试工具
老华带你飞2 小时前
婚纱摄影网站|基于java + vue婚纱摄影网站系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
浅川.252 小时前
STL专项:deque 双端队列
开发语言·c++·stl·deque
旅行的狮子2 小时前
5分钟快速体验Midscene.js(Node环境、Playwright)
开发语言·javascript·midscenejs