1764C.md (2578B)
1 Source: https://codeforces.com/problemset/problem/1764/C 2 3 C. Doremy's City Construction 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Doremy's new city is under construction! The city can be regarded as a simple undirected graph with $n$ vertices. The $i$-th vertex has altitude $a_i$. Now Doremy is deciding which pairs of vertices should be connected with edges. 22 23 Due to economic reasons, there should be no self-loops or multiple edges in the graph. 24 25 Due to safety reasons, there should not be pairwise distinct vertices $u$, $v$, and $w$ such that $a_u \leq a_v \leq a_w$ and the edges $(u,v)$ and $(v,w)$ exist. 26 27 Under these constraints, Doremy would like to know the maximum possible number of edges in the graph. Can you help her? 28 29 Note that the constructed graph is allowed to be disconnected. 30 31 Input 32 33 The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. 34 35 The first line of each test case contains a single integer $n$ ($2 \le n \le 2\cdot 10^5$) — the number of vertices. 36 37 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1\le a_i\le 10^6$) — the altitudes of each vertex. 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 40 41 Output 42 43 For each test case, output the maximum possible number of edges in the graph. 44 45 Example 46 47 Input 48 49 ``` 50 4 51 52 4 53 54 2 2 3 1 55 56 6 57 58 5 2 3 1 5 2 59 60 12 61 62 7 2 4 9 1 4 6 3 7 4 2 3 63 64 4 65 66 1000000 1000000 1000000 1000000 67 ``` 68 69 Output 70 71 ``` 72 3 73 74 9 75 76 35 77 78 2 79 ``` 80 81 Note 82 83 In the first test case, there can only be at most $3$ edges in the graph. A possible construction is to connect $(1,3)$, $(2,3)$, $(3,4)$. In the picture below the red number above node $i$ is $a_i$. 84 85  86 87 The following list shows all such $u$, $v$, $w$ that the edges $(u,v)$ and $(v,w)$ exist. 88 89 * $u=1$, $v=3$, $w=2$; 90 * $u=1$, $v=3$, $w=4$; 91 * $u=2$, $v=3$, $w=1$; 92 * $u=2$, $v=3$, $w=4$; 93 * $u=4$, $v=3$, $w=1$; 94 * $u=4$, $v=3$, $w=2$. 95 96 Another possible construction is to connect $(1,4)$, $(2,4)$, $(3,4)$. 97 98  99 100 An unacceptable construction is to connect $(1,3)$, $(2,3)$, $(2,4)$, $(3,4)$. Because when $u=4$, $v=2$, $w=3$, $a_u\le a_v \le a_w$ holds, and the respective edges exist. 101 102 