Codeforces Global Round 24
C
Doremy's City Construction
Codeforces
1400
原题
← BD →
翻译中…

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.

翻译中…

Due to economic reasons, there should be no self-loops or multiple edges in the graph.

翻译中…

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.

翻译中…

Under these constraints, Doremy would like to know the maximum possible number of edges in the graph. Can you help her?

翻译中…

Note that the constructed graph is allowed to be disconnected.

翻译中…

Input

翻译中…

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.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 2\cdot 10^5$) — the number of vertices.

翻译中…

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.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output the maximum possible number of edges in the graph.

翻译中…

Note

翻译中…

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$.

翻译中…

image

翻译中…

The following list shows all such $u$, $v$, $w$ that the edges $(u,v)$ and $(v,w)$ exist.

翻译中…
  • $u=1$, $v=3$, $w=2$;
  • $u=1$, $v=3$, $w=4$;
  • $u=2$, $v=3$, $w=1$;
  • $u=2$, $v=3$, $w=4$;
  • $u=4$, $v=3$, $w=1$;
  • $u=4$, $v=3$, $w=2$.
翻译中…

Another possible construction is to connect $(1,4)$, $(2,4)$, $(3,4)$.

翻译中…

image

翻译中…

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.

翻译中…

image

翻译中…

样例

Input #1
4
4
2 2 3 1
6
5 2 3 1 5 2
12
7 2 4 9 1 4 6 3 7 4 2 3
4
1000000 1000000 1000000 1000000
Output #1
3
9
35
2
InputCopy OutputCopy
题目信息
题号
1764C
时间限制
1 s
内存限制
256 MB
标签
图论
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。