我是靠谱客的博主 欢呼溪流,最近开发中收集的这篇文章主要介绍Codeforces A. Arrival of the General(贪心),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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-increasi

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+5;
int n,m,flag,flag1,ma,mi,ans;
int a[MAXN];
string str;
int main()
{
cin>>n;
ma=0;
mi=1000;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
if(a[i]>ma)
{
ma=a[i];
flag=i;
}
}
for(int i=n;i>0;i--)
{
if(a[i]<mi)
{
mi=a[i];
flag1=i;
}
}
//cout<<flag<<' '<<flag1<<endl;
ans=flag-1+n-flag1;
if(flag>flag1)
ans-=1;
cout<<ans<<endl;
return 0;
}

ng 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

Copy

4
33 44 11 22

output

Copy

2

input

Copy

7
10 10 58 31 63 40 76

output

Copy

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:

  1. (10, 10, 58, 31, 63, 40, 76)
  2. (10, 58, 10, 31, 63, 40, 76)
  3. (10, 58, 10, 31, 63, 76, 40)
  4. (10, 58, 10, 31, 76, 63, 40)
  5. (10, 58, 31, 10, 76, 63, 40)
  6. (10, 58, 31, 76, 10, 63, 40)
  7. (10, 58, 31, 76, 63, 10, 40)
  8. (10, 58, 76, 31, 63, 10, 40)
  9. (10, 76, 58, 31, 63, 10, 40)
  10. (76, 10, 58, 31, 63, 10, 40)
  11. (76, 10, 58, 31, 63, 40, 10)   
  12. 题意:把最高的数字移到最右边,最小的移到最左边需要几次,每次交换两个相邻的数字。需要考虑数字重复和最大数最小数位置的情况,选择最左边的最大数和最右边的最小数移动即刻。

最后

以上就是欢呼溪流为你收集整理的Codeforces A. Arrival of the General(贪心)的全部内容,希望文章能够帮你解决Codeforces A. Arrival of the General(贪心)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部