Suppose you have two points p(xp, yp) and q(xq, yq). The Manhattan distance between the points is given by d(p, q) = | xp - xq | + | yp - yq |. Lets call a triplet of points (p, q, r) unstable if d(p, q) = d(p, r) + d(q, r). An array A1, A2, ...An is called stable if it is impossible to pick three indices i, j, k such that the triplet of points [ (Ai, i), (Aj, j), (Ak, k) ] is unstable. You are given an array A1, A2, ...An. Calculate the number of stable subarrays of this array. A subarray of an array is defined as the array Al, Al + 1, ...Ar for some 1 <= l <= r <= n. Input The first line contains a single integer N - The size of the array The next line contains N space separated integers - the elements of the array. Output Print the number of stable subarrays of this array Constraints 1 <= N, A[i] <= 100000 Example Input 4 2 4 1 3 Output 10 Input 5 6 9 1 9 6 Output 12 Explanation Testcase 1: It can be shown that all subarrays of this array are stable. Testcase 2: Subarray [A1, A2, A3, A4] is unstable as [(A1, 1), (A2, 2), (A4, 4)] is a unstable triplet. in c++
Question
Suppose you have two points p(xp, yp) and q(xq, yq). The Manhattan distance between the points is given by d(p, q) = | xp - xq | + | yp - yq |.
Lets call a triplet of points (p, q, r) unstable if d(p, q) = d(p, r) + d(q, r). An array A1, A2, ...An is called stable if it is impossible to pick three indices
i, j, k such that the triplet of points [ (Ai, i), (Aj, j), (Ak, k) ] is unstable. You are given an array A1, A2, ...An. Calculate the number of stable subarrays of this array. A subarray of an array is defined as the array Al, Al + 1, ...Ar for some 1 <= l <= r <= n.
Input
The first line contains a single integer N - The size of the array
The next line contains N space separated integers - the elements of the array.
Output
Print the number of stable subarrays of this array
Constraints
1 <= N, A[i] <= 100000
Example
Input
4
2 4 1 3
Output
10
Input
5
6 9 1 9 6
Output
12
Explanation
Testcase 1: It can be shown that all subarrays of this array are stable.
Testcase 2: Subarray [A1, A2, A3, A4] is unstable as [(A1, 1), (A2, 2), (A4, 4)] is a unstable triplet. in c++
Solution
Here is a C++ solution for the problem:
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for(int i=0; i<n; i++)
cin >> a[i];
long long ans = 0;
for(int i=0; i<n; i++) {
int left = i-1, right = i+1;
while(left >= 0 && a[left] <= a[i]) {
left--;
}
while(right < n && a[right] <= a[i]) {
right++;
}
ans += (long long)(i-left)*(right-i);
}
cout << ans << "\n";
return 0;
}
This solution works by iterating over each element of the array and for each element, it finds the nearest element on the left and right that is greater than the current element. The number of stable subarrays that include the current element is the product of the distances to the nearest greater elements on the left and right. The total number of stable subarrays is the sum of the number of stable subarrays for each element.
Similar Questions
The Manhattan distance between two points (x1,y1)(𝑥1,𝑦1) and (x2,y2)(𝑥2,𝑦2) is defined as:|x1−x2|+|y1−y2|.|𝑥1−𝑥2|+|𝑦1−𝑦2|.We call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal.You are given a set of pairwise distinct points and an even integer d𝑑. Your task is to find any Manhattan triangle, composed of three distinct points from the given set, where the Manhattan distance between any pair of vertices is equal to d𝑑.InputEach test consists of multiple test cases. The first line contains one integer t𝑡 (1≤t≤1041≤𝑡≤104) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n𝑛 and d𝑑 (3≤n≤2⋅1053≤𝑛≤2⋅105, 2≤d≤4⋅1052≤𝑑≤4⋅105, d𝑑 is even) — the number of points and the required Manhattan distance between the vertices of the triangle.The (i+1)(𝑖+1)-th line of each test case contains two integers xi𝑥𝑖 and yi𝑦𝑖 (−105≤xi,yi≤105−105≤𝑥𝑖,𝑦𝑖≤105) — the coordinates of the i𝑖-th point. It is guaranteed that all points are pairwise distinct.It is guaranteed that the sum of n𝑛 over all test cases does not exceed 2⋅1052⋅105.OutputFor each test case, output three distinct integers i𝑖, j𝑗, and k𝑘 (1≤i,j,k≤n1≤𝑖,𝑗,𝑘≤𝑛) — the indices of the points forming the Manhattan triangle. If there is no solution, output "0 0 00 0 0" (without quotes).If there are multiple solutions, output any of them.ExampleinputCopy66 43 10 00 -25 -33 -52 -25 40 00 -25 -33 -52 -26 63 10 00 -25 -33 -52 -24 43 00 3-3 00 -310 82 1-5 -1-4 -1-5 -30 1-2 5-4 4-4 20 0-4 14 400000100000 100000-100000 100000100000 -100000-100000 -100000outputCopy2 6 14 3 53 5 10 0 06 1 30 0 0NoteIn the first test case:Points A𝐴, B𝐵, and F𝐹 form a Manhattan triangle, the Manhattan distance between each pair of vertices is 44. Points D𝐷, E𝐸, and F𝐹 can also be the answer.In the third test case:Points A𝐴, C𝐶, and E𝐸 form a Manhattan triangle, the Manhattan distance between each pair of vertices is 66.In the fourth test case, there are no two points with a Manhattan distance of 44, and therefore there is no suitable Manhattan triangle.
The distance D between two points with coordinates (x1,y1) and (x2,y2) on a plane is given byD = [(x2- x1)2 + (y2 – y1)2]1/2The distance between two points (x1,y1,z1) and (x2,y2,z2) in a three dimensional space is given byD = [(x2-x1)2 +(y2-y1)2 + (z2-z1)2]1/2Develop a program in C++ using function overloading by writing the same function ComputeDistance() with different signatures. Round off distance to two decimal pointsNote: Syntax to print 'x' decimal places of variable 'a'include <iomanip>usecout<<fixed<<setprecision(x)<<a;Input formatx coordinate of point 1y coordinate of point 1z coordinate of point 1x coordinate of point 2y coordinate of point 2z coordinate of point 2Output formatDistance in two dimensional planeDistance in three dimensional space
Question 6The Euclidean distance between two points will always be shorter than the Manhattan distance:1 pointTrueFalse
Which term best describes the set of all points in a plane for which the sum of the distances to two fixed points equals a certain constant?
Consider and to be two points on a 2D plane. happens to equal the minimum value in Northern Latitude (LAT_N in STATION). happens to equal the minimum value in Western Longitude (LONG_W in STATION). happens to equal the maximum value in Northern Latitude (LAT_N in STATION). happens to equal the maximum value in Western Longitude (LONG_W in STATION).Query the Manhattan Distance between points and and round it to a scale of decimal places.Input FormatThe STATION table is described as follows:
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.