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);
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Java核心技术之什么是泛型

    Java核心技术之什么是泛型没看过官网,不知道类型擦除会产生的问题还敢说自己了解泛型?

  • java反转数组_Java实现数组反转翻转的方法实例

    java反转数组_Java实现数组反转翻转的方法实例数组翻转的方法(java实现),数组翻转,就是将数组倒置,例如原数组为:{“a”,”b”,”c”,”d”},那么翻转后的数组为{“d”,”c”,”b”,”a”}。【方法一】使用集合个工具类:Collections.reverse(ArrayList)将数组进行反转:importjava.util.ArrayList;importjava.util.Collections;publiccl…

  • 获取的string转JSONArray或JSONObject

    获取的string转JSONArray或JSONObject² 返回值:JSON格式字符串{“serviceId”:”3c.park.queryparkstandard”,”resultCode”:0,”message”:”成功”,”dataItems”:[{“objectId”:””,”operateType”:”READ”,”attributes”:{“parkCode”:”park01″,

  • 防止网站被挂马_网站被挂马的原因

    防止网站被挂马_网站被挂马的原因今天遇到一个很奇怪的挂马问题,查关键词,查数据库等常规方法都没有找到原因,debug断点都放在了程序执行代码最前面还是输出挂马内容,用php探针发现也有代码,所以确认了是iis全局的问题,所以查加载查组件,最终经过比对是iis被黑添加了模块,被添加的名称很具有迷惑性,通常伪装的很像系统模块,遇到查不到是什么地方挂马,可以放一个探针来判断下是不是iis问题。»转载请保留出处:豫章小站…

  • 中文停用词_中文停用词哪个

    中文停用词_中文停用词哪个!"#$%&'()*+,…………………………../.一.数.日///0123456789:://::;<=>>&g

  • 100+Python编程题给你练(附答案)

    大家如果能坚持独立思考完成以下题目,一定可以帮大家轻松getPython的编程技能。目前,这个项目已经获得了3994Stars,2952Forks。Github地址:Python-programming-exercises首先,这100+练习题根据难易程度分为三个等级:Level1、2和3。下面对如何定义这三个Level进行了说明,大家可以结合自身的学习能…

发表回复

您的电子邮箱地址不会被公开。

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