CF1538A.Stone Game
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power ai. The powers of all stones are distinct.
Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.
Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.
For example, if n=5 and a=[1,5,4,3,2], then Polycarp could make the following moves:
- Destroy the leftmost stone. After this move a=[5,4,3,2];
- Destroy the rightmost stone. After this move a=[5,4,3];
- Destroy the leftmost stone. After this move a=[4,3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game.
Please note that in the example above, you can complete the game in two steps. For example:
- Destroy the leftmost stone. After this move a=[5,4,3,2];
- Destroy the leftmost stone. After this move a=[4,3,2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game.
Polycarp 正在玩一款新的电脑游戏。游戏中有 n 颗石头排成一排。位于位置 i 的石头具有整数能量值 ai,且所有石头的能量值互不相同。
每一轮,Polycarp 可以摧毁最左边(即第一个位置)或最右边(即最后一个位置)的石头(换句话说,只能摧毁当前序列的左端点或右端点)。当 Polycarp 摧毁一颗石头后,该石头便不再存在。
现在,Polycarp 希望获得两项成就:他只有在摧毁了能量值最小的石头和能量值最大的石头之后,才能达成这两项成就。请帮助 Polycarp 计算,为达成目标所需的最少操作次数。
例如,若 n=5 且 a=[1,5,4,3,2],则 Polycarp 可执行如下操作:
- 摧毁最左边的石头。此操作后 a=[5,4,3,2];
- 摧毁最右边的石头。此操作后 a=[5,4,3];
- 摧毁最左边的石头。此操作后 a=[4,3]。此时 Polycarp 已摧毁能量值最大和最小的石头,因此可以结束游戏。
请注意,在上述例子中,你也可以仅用两步完成游戏。例如:
- 摧毁最左边的石头。此操作后 a=[5,4,3,2];
- 摧毁最左边的石头。此操作后 a=[4,3,2]。此时 Polycarp 已摧毁能量值最大和最小的石头,因此可以结束游戏。
输入格式
The first line contains an integer t (1≤t≤100). Then t test cases follow.
The first line of each test case contains one integer n (2≤n≤100) — the number of stones.
The second line contains n distinct integers a1,a2,…,an (1≤ai≤n) — the power of the stones.
第一行包含一个整数 t(1≤t≤100)。随后是 t 组测试数据。
每组测试数据的第一行包含一个整数 n(2≤n≤100)—— 表示石头的数量。
第二行包含 n 个互不相同的整数 a1,a2,…,an(1≤ai≤n)—— 表示各石头的能量值。
输出格式
For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.
对于每个测试用例,输出摧毁功率最大和功率最小的石头所需的最少移动次数。
输入输出样例
输入#1
5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4
输出#1
2 4 5 3 2
输入解题思路,AI测评打分。不知道怎么写?