Arrival of the General

简介: Arrival of the General

文章目录

一、Arrival of the General

总结


一、Arrival of the General

本题链接:Arrival of the General


题目:

A. Arrival of the General

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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)

本博客给出本题截图

image.png

image.png

题意:只能交换相邻的两个元素,问最少经过多少次交换后,第一个元素为最大的,最后一个元素为最小的

AC代码

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int a[N];
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++ )
        cin >> a[i];
    int p = 0, q = N;
    int pi, qi;
    for (int i = 0; i < n; i ++ )
        if (a[i] > p)
        {
            p = a[i];
            pi = i;
        }
    for (int i = n - 1; i >= 0; i -- )
        if (a[i] < q)
        {
            q = a[i];
            qi = i;
        }
        if (pi > qi)
            cout << pi + n - 1 - qi - 1 << endl;
        else
            cout << pi + n - 1 - qi << endl;
    return 0;
}

总结

水题,不解释

目录
相关文章
|
7月前
|
关系型数据库 MySQL 数据库
Unknown collation: ‘utf8mb4_0900_ai_ci‘
Unknown collation: ‘utf8mb4_0900_ai_ci‘
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation
84 0
|
8月前
|
存储 自然语言处理 搜索推荐
mysql中utf8、utf8mb4和utf8mb4_unicode_ci、utf8mb4_general_ci
mysql中utf8、utf8mb4和utf8mb4_unicode_ci、utf8mb4_general_ci
277 0
|
SQL 存储 关系型数据库
CHARACTER SET utf8 COLLATE utf8_general_ci
CHARACTER SET utf8 COLLATE utf8_general_ci
|
SQL 关系型数据库 MySQL
Mysql报错:Illegal mix of collations (utf8mb4_bin,NONE) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation
Mysql报错:Illegal mix of collations (utf8mb4_bin,NONE) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation
674 0
|
关系型数据库 MySQL Shell
[ERROR] COLLATION ‘utf8_unicode_ci‘ is not valid for CHARACTER SET ‘latin1‘
[ERROR] COLLATION ‘utf8_unicode_ci‘ is not valid for CHARACTER SET ‘latin1‘
|
SQL 关系型数据库 MySQL
解决:[Err] 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘
记录如何结局:[Err] 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci’这个问题
509 1
解决:[Err] 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘
|
数据库
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE)
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/82016025 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation 'like' 这个问题一看就是编码的问题。
6392 0
|
SQL 关系型数据库 MySQL
mysql: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation &#39;= 的解决
昨天把mysql里所有table的varchar字段的字符集,批量换成了utf8mb4/utf8mb4_unicode_ci ,以便能保存一些emoji火星文 , 结果有一个sql语句执行时,报错如下: Illegalmixofcollations (utf8_unicode_ci,IMPLICI...
2156 0