题目描述
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.
By the military charter the soldiers should stand in the order of non-increasing of their height. But as there’s virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.
For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.
Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
Input
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, …, an (1 ≤ ai ≤ 100) the values of the soldiers’ heights in the order of soldiers’ heights’ increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, …, an are not necessarily different.
Output
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
样例
Examples
input
4
33 44 11 22
output
2
input
7
10 10 58 31 63 40 76
output
10
---------------------------------------
Note
In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).
In the second sample the colonel may swap the soldiers in the following sequence:
(10, 10, 58, 31, 63, 40, 76)
(10, 58, 10, 31, 63, 40, 76)
(10, 58, 10, 31, 63, 76, 40)
(10, 58, 10, 31, 76, 63, 40)
(10, 58, 31, 10, 76, 63, 40)
(10, 58, 31, 76, 10, 63, 40)
(10, 58, 31, 76, 63, 10, 40)
(10, 58, 76, 31, 63, 10, 40)
(10, 76, 58, 31, 63, 10, 40)
(76, 10, 58, 31, 63, 10, 40)
(76, 10, 58, 31, 63, 40, 10)
题目翻译:
一个国防部派了一位将军来视察超级秘密军事小队,由SuperDuper上校指挥。得知这一消息后,上校命令所有N个小队的士兵在阅兵场上列队。
根据军事章程,士兵们应该按照身高不增加的顺序站立。但由于几乎没有时间,士兵们按照任意的顺序排队。然而,将军是个短视的人,他认为如果队伍中第一个士兵的身高最大,最后一个士兵的身高最小,那么士兵们的排队就正确了。请注意,其他士兵的排列方式并不重要,包括有几个士兵的高度是最大或最小的情况。只有第一个和最后一个士兵的高度是重要的。
例如,将军认为高度顺序(4,3,4,2,1,1)正确,顺序(4,3,1,2,2)错误。
在一秒钟内,上校可以调换任何两个相邻的士兵。请帮助他计算出形成将军认为正确的阵容所需的最少时间。
输入
第一行输入的是唯一的整数n(2≤n≤100),代表队伍中士兵的数量。第二行包含整数a1, a2, …, an (1 ≤ ai ≤ 100),代表士兵的高度,按士兵的高度从行首到行尾的顺序递增。这些数字是以空格分隔的。数字a1,a2,…,an不一定不同。
输出
打印唯一的整数–上校形成将军喜欢的阵容所需的最小秒数。
算法1
模拟
找出最靠右的最小值下标和最靠左的最大值下标。
C++ 代码
#include <bits/stdc++.h>
using namespace std;
int a[110];
int mins=110,maxs=0,imin,imax;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for (int i = 1; i <= n; i ++ )
{
if (a[i] > maxs)
{
maxs = a[i];
imax = i;
}
}
for (int i = n; i >= 1; i -- )
{
if (a[i] < mins)
{
mins = a[i];
imin = i;
}
}
int ans = imax - 1 + n - imin; //ans = 往左移次数+往右移次数
if (imax > imin) cout << ans - 1 << endl; //两者在移动过程中有交换,则答案数-1
else cout << ans << endl; //否则直接输出即可
return 0;
}