Problem description

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:

  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)

解题思路:题目的意思就是规定正确的序列中最大值在第一个位置,最小值在最后一个位置。要求将不满足序列中的元素采用最小交换次数将其变成正确的序列。做法:先找出最小值的下标k,注意a[k]>=a[i](等号要加上),再将k后面的元素往前移一位,再去找最大值的下标即可,水过!

AC代码:

 #include<bits/stdc++.h>
using namespace std;
int main(){
int n,m=,k=,a[];
cin>>n;
for(int i=;i<n;++i)cin>>a[i];
for(int i=;i<n;++i)
if(a[k]>=a[i])k=i;//先找出最小值的下标
m+=n-k-;
for(int i=k;i<n-;++i)a[i]=a[i+];
k=;
for(int i=;i<n-;++i)
if(a[k]<a[i])k=i;//找最大值的下标
m+=k;
cout<<m<<endl;
return ;
}

C - Arrival of the General的更多相关文章

  1. codeforces Arrival of the General 题解

    A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...

  2. mysql general log日志

    注:应一直出现http://www.cnblogs.com/hwaggLee/p/6030765.html文章中的问题 故mysql general log日志.查看具体是什么命令导致的. 打开 ge ...

  3. Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”

    Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为"通用职责分配软件模式" 1. GRA ...

  4. OpenCASCADE General Transformation

    OpenCASCADE General Transformation eryar@163.com Abstract. OpenCASCADE provides a general transforma ...

  5. RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

    前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...

  6. 地理信息系统 - ArcGIS - 高/低聚类分析工具(High/Low Clustering ---Getis-Ord General G)

    前段时间在学习空间统计相关的知识,于是把ArcGIS里Spatial Statistics工具箱里的工具好好研究了一遍,同时也整理了一些笔记上传分享.这一篇先聊一些基础概念,工具介绍篇随后上传. 空间 ...

  7. java-collections.sort异常Comparison method violates its general contract!

    转载:http://www.tuicool.com/articles/MZreyuv 异常信息 java.lang.IllegalArgumentException: Comparison metho ...

  8. compass General 常用api学习[Sass和compass学习笔记]

    compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...

  9. c++错误——intermediate.manifest : general error c1010070很傻的错

    .\Debug\sadf.exe.intermediate.manifest : general error c1010070: Failed to load and parse the manife ...

随机推荐

  1. Math.floor() 与 parseInt()

    parseInt()与Math.floor()都能实现数字的向下取整,但是两者存在根本上的差异,1.Math.floor()用于一个数的向下取整,不能解析字符串 <script type=&qu ...

  2. spine骨骼动画组件使用详解

    1. spine骨骼动画工具 骨骼动画: 把动画打散, 通过工具,调骨骼的运动等来形成动画spine是一个非常流行的2D骨骼动画制作工具spine 动画美术人员导出3个文件:    (1) .png文 ...

  3. SqlServer 【基 本 操 作】

    1.Row_Number() select * from (select Row_Number() over (order by FSalary) as 'RowNum' ,* from dbo.T_ ...

  4. poj2385 - Apple Catching【动态规划】

    Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...

  5. Django CBV视图解决csrf认证

    urls.py from django.conf.urls import url from appxx import views urlpatterns = [ url(r"^$" ...

  6. Linux - docker基础

    目录 Linux - docker基础 docker的概念 docker安装流程 docker基本命令学习 docker 的 hello docker 运行一个ubuntu容器 Docker与Cent ...

  7. Leetcode 48.旋转矩阵

    旋转矩阵 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...

  8. [Cogs14] [网络流24题#1] 飞行员分配方案 [网络流,最大流,二分图匹配]

    经典二分图匹配,可以用匈牙利算法,也可以用最大流 代码如下(Dinic): #include <iostream> #include <cstdio> #include < ...

  9. nyoj_289_苹果_20140307

    苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ctest有n个苹果,要将它放入容量为v的背包.给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值. ...

  10. [转]SQLSERVER一些公用DLL的作用解释

    转自:Leo_wlCnBlogs SQLSERVER一些公用DLL的作用解释 如果你的SQLSERVER安装在C盘的话,下面的路径就是相应SQLSERVER版本的公用DLL的存放路径 SQL2005 ...