CF1696D.Permutation Graph

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

You are given a permutation of 1,2,…,n1,2,\dots,n, [a1,a2,…,an][a_1,a_2,\dots,a_n]. For integers ii, jj such that 1≤i<j≤n1\le i \lt j\le n, define mn⁡(i,j)\operatorname{mn}(i,j) as min⁡k=ijak\min\limits_{k=i}^j a_k, and define mx⁡(i,j)\operatorname{mx}(i,j) as max⁡k=ijak\max\limits_{k=i}^j a_k.

Let us build an undirected graph of nn vertices, numbered 11 to nn. For every pair of integers 1≤i<j≤n1\le i \lt j\le n, if mn⁡(i,j)=ai\operatorname{mn}(i,j)=a_i and mx⁡(i,j)=aj\operatorname{mx}(i,j)=a_j both holds, or mn⁡(i,j)=aj\operatorname{mn}(i,j)=a_j and mx⁡(i,j)=ai\operatorname{mx}(i,j)=a_i both holds, add an undirected edge of length 11 between vertices ii and jj.

In this graph, find the length of the shortest path from vertex 11 to vertex nn. We can prove that 11 and nn will always be connected via some path, so a shortest path always exists.

排列是指由 11 到 nn 的 nn 个互不相同的整数组成的数组,顺序任意。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

给定一个 1,2,…,n1,2,\dots,n 的排列 [a1,a2,…,an][a_1,a_2,\dots,a_n]。对于满足 1≤i<j≤n1\le i \lt j\le n 的整数 i,ji,j,定义 mn⁡(i,j)=min⁡k=ijak\operatorname{mn}(i,j)=\min\limits_{k=i}^j a_k,并定义 mx⁡(i,j)=max⁡k=ijak\operatorname{mx}(i,j)=\max\limits_{k=i}^j a_k。

我们构造一个包含 nn 个顶点(编号为 11 到 nn)的无向图。对每一对满足 1≤i<j≤n1\le i \lt j\le n 的整数 i,ji,j,若同时满足 mn⁡(i,j)=ai\operatorname{mn}(i,j)=a_i 且 mx⁡(i,j)=aj\operatorname{mx}(i,j)=a_j,或同时满足 mn⁡(i,j)=aj\operatorname{mn}(i,j)=a_j 且 mx⁡(i,j)=ai\operatorname{mx}(i,j)=a_i,则在顶点 ii 和顶点 jj 之间添加一条长度为 11 的无向边。

在此图中,求从顶点 11 到顶点 nn 的最短路径长度。可以证明:顶点 11 和顶点 nn 总是通过某条路径连通,因此最短路径一定存在。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5⋅1041 \le t \le 5\cdot 10^4). Description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤2.5⋅1051\le n\le 2.5\cdot 10^5).

The second line of each test case contains nn integers a1a_1, a2a_2, …\ldots, ana_n (1≤ai≤n1\le a_i\le n). It's guaranteed that aa is a permutation of 11, 22, …\dots, nn.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5⋅1041 \le t \le 5\cdot 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2.5⋅1051\le n\le 2.5\cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 a1a_1, a2a_2, …\ldots, ana_n(1≤ai≤n1\le a_i\le n)。保证 aa 是 11, 22, …\dots, nn 的一个排列。

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

输出格式

For each test case, print a single line containing one integer — the length of the shortest path from 11 to nn.

对于每个测试用例,输出一行,包含一个整数——从节点 11 到节点 nn 的最短路径长度。

输入输出样例

  • 输入#1

    5
    1
    1
    2
    1 2
    5
    1 4 2 3 5
    5
    2 1 5 3 4
    10
    7 4 8 1 6 10 3 5 2 9

    输出#1

    0
    1
    1
    4
    6

说明/提示

The following are illustrations of constructed graphs in example test cases.

the constructed graph in test case 1

the constructed graph in test case 2

the constructed graph in test case 3

the constructed graph in test case 4

the constructed graph in test case 5

以下是示例测试用例中构造的图的示意图。

测试用例 1 中构造的图

测试用例 2 中构造的图

测试用例 3 中构造的图

测试用例 4 中构造的图

测试用例 5 中构造的图

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

首页