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


相关推荐

  • 画中画广告..例

    画中画广告..例<!–画中画广告开始–><tableborder=0cellspacing=0cellpadding=0align=left><tr><td&gt

    2022年7月1日
    22
  • scratch编程小游戏黑白棋

    scratch编程小游戏黑白棋你有没有玩过一种游戏,就是按3X3排列的方块,方块中有黑有白,当你按下一个后周围的黑块会变成白块,白块变黑块,当全部的方块都变成白色是就赢了,今天我们就来编写这个游戏!首先来看看效果:知道怎么玩了吗?现在就来看看是怎么编写的吧!首先画出所有的角色:方块要画出一黑一白:方块首先要移动到合适的位置,然根据探测器来切换黑白:方块1:方块2:方块3:方块4:方块5:方块6:方块7:方块8:方块9:现在是最最最最重要的探测器程序了,探测器决定了布局的结果,保证不会

    2022年6月15日
    53
  • fastclick使用与解密

    fastclick使用与解密fastclick可以消除点击延时提高程序的运行效率。FastClick是一个非常方便的库,在移动浏览器上发生介于轻敲及点击之间的指令时,能够让你摆脱300毫秒的延迟。FastClick可以让你的应用程序更加灵敏迅捷。支持各种移动浏览器,比如Safari、Chrome、Opera等。

    2022年6月19日
    26
  • navicat12激活码[在线序列号]

    navicat12激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    1.1K
  • Git删除本地分支_idea删除git分支

    Git删除本地分支_idea删除git分支引言:注:本人一直都是用的gitbash窗口完成日常的开发工作。事情是这样的,切换分支的时候命令打错了,gitcheckout后面没有跟分支名,结果gitstatus,很多delete的文件,直接冒冷汗,gitadd,commit之后发现本地与远程确实是删除了很多文件,我本地没有修改的代码,于是选择直接删除本地的分支,然后重新从远程拉分支。具体操作:我现在在dev20181018分支…

    2022年10月16日
    5
  • Not enough information to list image symbols. Not enough information to list load addresses in …「建议收藏」

    Not enough information to list image symbols. Not enough information to list load addresses in …「建议收藏」linking…..\Objects\BBQ_Wifi.axf:Error:L6218E:UndefinedsymbolClear_Led_Timer(referredfrommain.o)…\Objects\BBQ_Wifi.axf:Error:L6218E:UndefinedsymbolGet_Led_Timer(referredfrommain.o…

    2022年9月17日
    6

发表回复

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

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