CF1550C.Manhattan Subarrays
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Suppose you have two points p=(xp,yp) and q=(xq,yq). Let's denote the Manhattan distance between them as d(p,q)=∣xp−xq∣+∣yp−yq∣.
Let's say that three points p, q, r form a bad triple if d(p,r)=d(p,q)+d(q,r).
Let's say that an array b1,b2,…,bm is good if it is impossible to choose three distinct indices i, j, k such that the points (bi,i), (bj,j) and (bk,k) form a bad triple.
You are given an array a1,a2,…,an. Calculate the number of good subarrays of a. A subarray of the array a is the array al,al+1,…,ar for some 1≤l≤r≤n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
假设你有两个点 p=(xp,yp) 和 q=(xq,yq)。我们记它们之间的曼哈顿距离为 d(p,q)=∣xp−xq∣+∣yp−yq∣。
若三个点 p、q、r 满足 d(p,r)=d(p,q)+d(q,r),则称它们构成一个坏三元组(bad triple)。
若对于数组 b1,b2,…,bm,无法选出三个互不相同的下标 i、j、k,使得点 (bi,i)、(bj,j) 和 (bk,k) 构成一个坏三元组,则称该数组为好数组(good array)。
现给定一个数组 a1,a2,…,an。请计算 a 的好子数组(good subarray)的个数。数组 a 的一个子数组定义为对某个满足 1≤l≤r≤n 的 l,r,所构成的数组 al,al+1,…,ar。
注意:根据定义,长度为 1 和 2 的子数组均为好子数组。
输入格式
The first line contains one integer t (1≤t≤5000) — the number of test cases.
The first line of each test case contains one integer n (1≤n≤2⋅105) — the length of array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
It's guaranteed that the sum of n doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤5000)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, print the number of good subarrays of array a.
对于每个测试用例,输出数组 a 中“好”子数组的个数。
输入输出样例
输入#1
3 4 2 4 1 3 5 6 9 1 9 6 2 13 37
输出#1
10 12 3
说明/提示
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a2,a3,a4] is good since it contains only three elements and:
- d((a2,2),(a4,4))=∣4−3∣+∣2−4∣=3 $ \lt $ d((a2,2),(a3,3))+d((a3,3),(a4,4))=3+1+2+1=7;
- d((a2,2),(a3,3)) $ \lt $ d((a2,2),(a4,4))+d((a4,4),(a3,3));
- d((a3,3),(a4,4)) $ \lt $ d((a3,3),(a2,2))+d((a2,2),(a4,4));
In the second test case, for example, subarray [a1,a2,a3,a4] is not good, since it contains a bad triple (a1,1), (a2,2), (a4,4):
- d((a1,1),(a4,4))=∣6−9∣+∣1−4∣=6;
- d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4;
- d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=2;
So, d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4)).
在第一个测试用例中,可以证明数组 a 的任意子数组都是“好”的。例如,子数组 [a2,a3,a4] 是“好”的,因为它仅包含三个元素,且满足:
- d((a2,2),(a4,4))=∣4−3∣+∣2−4∣=3 $ \lt $ d((a2,2),(a3,3))+d((a3,3),(a4,4))=3+1+2+1=7;
- d((a2,2),(a3,3)) $ \lt $ d((a2,2),(a4,4))+d((a4,4),(a3,3));
- d((a3,3),(a4,4)) $ \lt $ d((a3,3),(a2,2))+d((a2,2),(a4,4));
在第二个测试用例中,例如子数组 [a1,a2,a3,a4] 不是“好”的,因为它包含一个“坏”三元组 (a1,1)、(a2,2)、(a4,4):
- d((a1,1),(a4,4))=∣6−9∣+∣1−4∣=6;
- d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4;
- d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=2;
因此,d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4))。
输入解题思路,AI测评打分。不知道怎么写?