codeforces Arrival of the General 题解

codeforces 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-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.

Sample test(s)
input
4
33 44 11 22

output
2

input
7
10 10 58 31 63 40 76

output
10

相当于一个简单的冒泡排序了,只是不用直接排序,仅仅是计算一下而已。

注意

1 最大值和最小值交换的时候能够降低一次交换的。

2 元素是会反复的。

#include <iostream>
using namespace std;

namespace{
	static const int MAX_VAL = (int) 1E9;
	static const int MIN_VAL = (int) 1E-9;
}
void ArrivaloftheGeneral()
{
	int n, max_i, min_i, max_n = MIN_VAL, min_n = MAX_VAL, a;
	cin>>n;
	for (unsigned i = 0; i < n; i++)
	{
		cin>>a;
		if (a <= min_n)
		{
			min_n = a;
			min_i = i;
		}
		if (a > max_n)
		{
			max_n = a;
			max_i = i;
		}
	}
	cout<<max_i + (n - min_i - 1) - (max_i > min_i);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115713.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数组 – 稀疏数组

    一,稀疏数组1.定义稀疏数组可以看做是普通数组的压缩,但是这里说的普通数组是值无效数据量远大于有效数据量的数组形如:00000000000001000000000000200000000000000…

    2022年4月5日
    21
  • uint32-t_c语言uint32_t类型

    uint32-t_c语言uint32_t类型uint32_tu:代表unsigned即无符号,即定义的变量不能为负数;int:代表类型为int整形;32:代表四个字节,即为int类型;_t:代表用typedef定义的;整体代表:用typedef定义的无符号int型宏定义;uint8_t:u:代表unsigned即无符号,即定义的变量不能为负数;int:代表类型为int整形;8:代表一个字节…

    2022年9月7日
    0
  • linux下ll命令_ubuntu bash命令

    linux下ll命令_ubuntu bash命令ubuntull命令1、用过Redhat或者Centos的朋友应该很熟悉ll这个命令,就相当于ls-l,但在Ubuntu中要么不能用,要么会显示隐藏文件,看起来很烦。2、严格来说ll不是一个命令,只是命令的别名而已。很多Linux用户都使用bashshell,对普通用户来说用得最多的就是命令补全(按tab键)和alias(别名)功能。Ubunt…

    2022年9月24日
    0
  • java多线程—java线程的创建和线程的生命周期

    java线程的创建和线程的生命周期

    2022年2月24日
    46
  • 查看是否安装了webpack_webpack不是内部或外部命令

    查看是否安装了webpack_webpack不是内部或外部命令查看当前项目webpack版本在项目package.json中的scripts脚本命令中写入以下内容”webpack”:”webpack–version”然后打开终端在项目根路径下运行:npmrunwebpack

    2022年8月10日
    25
  • 如何防止135端口入侵「建议收藏」

    如何防止135端口入侵「建议收藏」
    新学期到了,许多学生都要配机,新电脑的安全防卫做好了吗?能不能拒绝成为黑客的肉鸡?令人遗憾的是,很多新手都不知道或者忽视了对敏感端口的屏蔽。例如135端口,一旦黑客利用135端口进入你的电脑,就能成功地控制你的机子。我们应该如何防范通过135端口入侵呢?下面我们就为大家来揭开谜底。

      小知识:每台互联网中的计算机系统,都会同时打开多个网络端口,端口就像出入房间的门一样。因为房间的门用于方便人们的进出,而端口则为不同的网路服务提供数据交换。正如房间的门可以放进小tou一样

    2022年10月28日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号