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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IDEA安装yarn

    IDEA安装yarn当我们拿到一个vue项目,导入到idea后发现有yarn.lock文件,则该前端项目需要通过yarn来进行启动。如下图:未安装前,如图下:安装步骤:1.各位看官注意,我们安装yarn,必须得把Nodejs的环境先配置好。以及npm的淘宝镜像安装好,也就是说启动vue的环境的是正常的。2.在Terminal面板下,通过cnpminstall-gyarn命令进行全局安装:3.执…

    2022年5月9日
    360
  • java常量池在方法区还是堆_JAVA常量池

    java常量池在方法区还是堆_JAVA常量池要是没有实践过别人书本上的理论的话,就还是会说常量池在方法区里面,要是知道方法区已经随jdk升级,被逐步干掉的话,就会看到有的文章说移动到heap堆里面了,还有极少的说移动到Metaspace里面了,产生了分歧。这个时候就需要实践出真知了。/***测试常量池在分区的位置**@authorLiXuekaion2020/6/9*/publicclassStringConstantPoolTest{publicstaticvoidmain(String[]

    2022年7月28日
    46
  • IDEA中,java项目无法使用Test测试的解决办法

    IDEA中,java项目无法使用Test测试的解决办法一、IDEA使用junit的@Test注解报错1、File–ProjectStructure–Modules2、点击加号3、选择JARsordirectories…4、在idea的安装路径下的lib文件夹,选中两个jar包5、然后勾选上,点击Apply–ok二、使用junit无法在控制台进行输入1、Help–EditCustomVMOptions..2、添加代码-Deditable.java….

    2022年10月17日
    4
  • php设计模式总结-工厂模式

    php设计模式总结-工厂模式

    2022年3月12日
    33
  • Docker安装RabbitMQ并安装延时队列插件

    Docker安装RabbitMQ并安装延时队列插件一、RabbitMQ简介RabbitMQ是由erlang语言开发,基于AMQP(AdvancedMessageQueue高级消息队列协议)协议实现的消息队列,它是一种应用程序之间的通信方法,消息队列在分布式系统开发中应用非常广泛。二、docker安装RabbitMQ1、搜索镜像dockersearchrabbitmq2、拉取并运行容器dockerrun-dit–namerabbitmq-eRABBITMQ_DEFAULT_USER=guest-eR

    2022年5月10日
    98
  • mybatis(pagehelper) dataTables实现分页功能

    mybatis(pagehelper) dataTables实现分页功能

    2021年5月12日
    175

发表回复

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

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