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)
上一篇 2022年1月31日 下午5:00
下一篇 2022年1月31日 下午5:00


相关推荐

  • Metro风格用户界面设计原则

    Metro风格用户界面设计原则这是翻译的一篇国外论坛上的文章 并不是很准确 想看原文的可以到这个网址 http www stephanemass com metro design principles nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp Metro 用户界面设计原则 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 注 MetroUI Metro 风格用户界面 即所谓的 Win8 风格 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 自从我在伦敦开始为用户贴近 MetroUI 和 Is

    2026年3月18日
    2
  • grok账号怎么支付充值会员?

    grok账号怎么支付充值会员?

    2026年3月15日
    2
  • 【Verilog-19.3】define和undef的用法

    【Verilog-19.3】define和undef的用法19 3 defineand undef 提供了文本宏替换功能 可以使用有意义的名称来表示常用的文本片段 例如 在整个描述中重复使用一个常数的情况下 文本宏是有用的 如果常数的值需要改变 因为它只需要更改源描述中的一个位置 文本宏工具不受编译器指令 resetall 的影响 19 3 1 define 指令 define 为文本替换创建了一个宏 这个指令可以在模块定义的内部和外部使用 一个文本宏定义以后 通过使用 字符 后面跟着宏的名字 它可以在源代码描述中使用 编译器应该用宏的文本替换字符串 t

    2026年3月18日
    2
  • mysql手机号段地区库_最新全国手机号段归属地数据库下载(mysql+xlsx+txt格式)46万条…「建议收藏」

    mysql手机号段地区库_最新全国手机号段归属地数据库下载(mysql+xlsx+txt格式)46万条…「建议收藏」在做网站程序时,经常用到校验用户输入的手机号归属地或所属移动,电信,联通等服务商,本手机号码段归属地数据库包括支持可查省份城市运营商邮编区号。而且提供.mysql,excel,txt三种数据格式数据库,2020年11月份最新的包括46万条记录集,可方便为实现手机号码段归属地查询提供强大后台数据库支持。三大运营商最新号段移动号段:134135136137138139147148…

    2022年7月22日
    18
  • Idea激活码最新教程2022.2.2版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2022.2.2版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2022 2 2 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2022 2 2 成功激活

    2025年5月25日
    4
  • 网易蜂巢基于万节点kubernetes支撑大规模云应用实践

    网易蜂巢基于万节点kubernetes支撑大规模云应用实践导读网易蜂巢是做容器 Docker 的 用 Kubernetes 来管理容器 现在蜂巢已经支撑了内部 外部很大规模的云计算应用 所以我们这个题目有两个关键 一个是 Kubernetes 和容器 另外一个是大规模云应用 网易蜂巢的大规模容器平台 nbsp 上图展示了蜂巢发展历程 其实很早就开始做蜂巢了 一开始从私有云开始建设 发展分成两层 应用层 平台层 进行了四个方面

    2026年3月19日
    3

发表回复

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

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