CF1764C.Doremy's City Construction

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Doremy's new city is under construction! The city can be regarded as a simple undirected graph with nn vertices. The ii-th vertex has altitude aia_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 uu, vv, and ww such that au≤av≤awa_u \leq a_v \leq a_w and the edges (u,v)(u,v) and (v,w)(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.

Doremy 的新城市正在建设中!该城市可被视作一个具有 nn 个顶点的简单无向图。第 ii 个顶点的海拔高度为 aia_i。现在 Doremy 正在决定哪些顶点对之间应连边。

出于经济原因,图中不允许存在自环或重边。

出于安全原因,图中不能存在三个互不相同的顶点 uu、vv 和 ww,使得 au≤av≤awa_u \leq a_v \leq a_w,且边 (u,v)(u,v) 与 (v,w)(v,w) 均存在。

在满足上述约束条件下,Doremy 想知道图中可能的最大边数。你能帮她吗?

注意:所构造的图允许不连通。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041\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 nn (2≤n≤2⋅1052 \le n \le 2\cdot 10^5) — the number of vertices.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1061\le a_i\le 10^6) — the altitudes of each vertex.

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

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2\cdot 10^5),表示顶点的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1061\le a_i\le 10^6),表示每个顶点的海拔高度。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出图中可能的最大边数。

输入输出样例

  • 输入#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

    输出#1

    3
    9
    35
    2

说明/提示

In the first test case, there can only be at most 33 edges in the graph. A possible construction is to connect (1,3)(1,3), (2,3)(2,3), (3,4)(3,4). In the picture below the red number above node ii is aia_i.

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

  • u=1u=1, v=3v=3, w=2w=2;
  • u=1u=1, v=3v=3, w=4w=4;
  • u=2u=2, v=3v=3, w=1w=1;
  • u=2u=2, v=3v=3, w=4w=4;
  • u=4u=4, v=3v=3, w=1w=1;
  • u=4u=4, v=3v=3, w=2w=2.

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

An unacceptable construction is to connect (1,3)(1,3), (2,3)(2,3), (2,4)(2,4), (3,4)(3,4). Because when u=4u=4, v=2v=2, w=3w=3, au≤av≤awa_u\le a_v \le a_w holds, and the respective edges exist.

在第一个测试用例中,图中最多只能有 33 条边。一种可能的构造方式是连接边 (1,3)(1,3)、(2,3)(2,3) 和 (3,4)(3,4)。下图中节点 ii 上方的红色数字为 aia_i。

以下列表列出了所有满足边 (u,v)(u,v) 与 (v,w)(v,w) 均存在的 uu、vv、ww:

  • u=1u=1,v=3v=3,w=2w=2;
  • u=1u=1,v=3v=3,w=4w=4;
  • u=2u=2,v=3v=3,w=1w=1;
  • u=2u=2,v=3v=3,w=4w=4;
  • u=4u=4,v=3v=3,w=1w=1;
  • u=4u=4,v=3v=3,w=2w=2。

另一种可能的构造方式是连接边 (1,4)(1,4)、(2,4)(2,4) 和 (3,4)(3,4)。

一种不可接受的构造方式是连接边 (1,3)(1,3)、(2,3)(2,3)、(2,4)(2,4) 和 (3,4)(3,4)。因为当 u=4u=4、v=2v=2、w=3w=3 时,满足 au≤av≤awa_u\le a_v \le a_w,且对应的边均存在。

输入解题思路,AI测评打分。不知道怎么写?

首页