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


相关推荐

  • R语言建立回归分析,并利用VIF查看共线性问题的例子「建议收藏」

    R语言建立回归分析,并利用VIF查看共线性问题的例子「建议收藏」使用R对内置longley数据集进行回归分析,如果以GNP.deflator作为因变量y,问这个数据集是否存在多重共线性问题?应该选择哪些变量参与回归?答:##查看longley的数据结构str(longley)##’data.frame’:  16obs.of  7variables:##  $GNP.deflator:num  8388.588

    2022年6月4日
  • 编写测试用例及一个例子

    编写测试用例及一个例子一.概念1.什么是测试用例?在测试过程中很重要的一类文档,它是测试工作的核心、是一组在测试时输入输出的标准、是软件需求的具体对照。2.测试用例的作用:检验软件是否满足客户需求、体现一个测试人员的工作量、展现测试用例的设计思路3.测试用例所包含的内容:用例编号、用例名称、测试背景、前置条件、优先级、重要级、测试数据、测试步骤、预期结果、实际结果、备注。4.测试用例的编写流…

    2022年7月17日
  • SpringCloud详细教程(上)

    SpringCloud详细教程(上)SpringCloud详细教程,SpringCloud详细教程。SpringCloud是一系列框架的有序集合。如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用SpringBoot的开发风格做到一键启动和部署。本文主要介绍了SpringCloud的核心组件以及如何基于SpringCloud构建微服务。

    2022年5月24日
  • stm32中u8,u16,u32的理解[通俗易懂]

    stm32中u8,u16,u32的理解[通俗易懂]u8是unsignedcharu16是unsignedshortu32是unsignedint这些都属於变量类型

    2022年10月15日
  • 在SQL存储过程中给条件变量加上单引号

    在SQL存储过程中给条件变量加上单引号,不加语句就会出问题,以下就是在存储过程中将条件where设置成了动态变化的,给where赋完值再和前面的语句拼接,再execute(SQL)@d_start

    2021年12月24日
  • 定制CentOS5.6系统

    定制CentOS5.6系统

    2021年8月13日

发表回复

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

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