Codeforces 144A 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 a 1, a 2, ..., a n (1 ≤ a i ≤ 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 a 1, a 2, ..., a n 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.
Examples
input
4
33 44 11 22
output
2
input
7
10 10 58 31 63 40 76
output
Copy
10
Note
In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).
In the second sample the colonel may swap the soldiers in the following sequence:
- (10, 10, 58, 31, 63, 40, 76)
- (10, 58, 10, 31, 63, 40, 76)
- (10, 58, 10, 31, 63, 76, 40)
- (10, 58, 10, 31, 76, 63, 40)
- (10, 58, 31, 10, 76, 63, 40)
- (10, 58, 31, 76, 10, 63, 40)
- (10, 58, 31, 76, 63, 10, 40)
- (10, 58, 76, 31, 63, 10, 40)
- (10, 76, 58, 31, 63, 10, 40)
- (76, 10, 58, 31, 63, 10, 40)
- (76, 10, 58, 31, 63, 40, 10)
题意
为数据排序,移动最大值到最前方,最小值到最后一位
输入的时候不断比较,但在出现等于最小值时更新位置(让定位更接近最后一位)
当最大值在最小值后时,总移动一次数 - 1
#include<bits/stdc++.h>
using namespace std;
int n, i, p, q = 99, x, a, b;
int main() {
for (cin >> n; i++ < n;)
cin >> x, x > p ? p = x, b = i : 0, x <= q ? q = x, a = i : 0;
cout << b + n - a - 1 - (b > a);//判断最大值是否在最小值后
}
Codeforces 144A 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 o ...
- Educational Codeforces Round 7 B. The Time 水题
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- C - Arrival of the General
Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...
- codeforces 712B B. Memory and Trident(水题)
题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...
- codeforces 712A A. Memory and Crow(水题)
题目链接: A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- codeforces 711A A. Bus to Udayland(水题)
题目链接: A. Bus to Udayland 题意: 找一对空位坐下来,水; 思路: AC代码: #include <iostream> #include <cstdio> ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- .NET8:快速集成Rapid.NET三维控件
.NET8正式版本发布了,AnyCAD Rapid.NET针对.NET8进行了升级和优化.本文以WPF项目为例介绍在.NET8中使用AnyCAD Rapid.NET三维控件. 1 从.NET6升级 若 ...
- Educational Codeforces Round 101 (Rated for Div. 2) E - A Bit Similar
题目传送门 很巧妙的一道题.对于一个 \(n\)位的 \(01\)字符串,一共有 \(2^n\)种不同字符排列,对于任意一个固定排列,在 \(2^n\)种排列中只有一种排列与该固定排列处处不等,而题干 ...
- Go笔记(1)-变量的详细用法
变量 (1)变量的定义 Go语言是静态类型的语言,所有类型都需要明确的定义. var是声明变量的关键字 使用格式:var 变量名 变量类型 变量命名规范:遵循驼峰格式,首个单词小写,每个新单词的首字母 ...
- java String字符串总结
这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...
- 循环依赖导致编译或者服务启动报错问题:The dependencies of some of the beans in the application context form a cycle
错误如图: 我的是服务器启动服务时报错: ***************************APPLICATION FAILED TO START************************* ...
- 效率工具:Hutool 嘎嘎香,被秀到了!
在日常开发中,我们会使用很多工具类来提升项目开发的速度,而国内用的比较多的 Hutool 框架,就是其中之一. 先来看官方对于 Hutool 的定义: Hutool 是一个小而全的 Java 工具类库 ...
- MySQL5.7允许远程root访问
MySQL5.7允许远程root访问 登录你的服务器MySQL mysql -u root -p MySQLroot密码 GRANT ALL PRIVILEGES ON *.* TO 'root'@' ...
- 网安区过年-Log4j2
Log4j2-2021 漏洞原理 Apache Log4j 2 是Java语言的日志处理套件,使用极为广泛.在其2.0到2.14.1版本中存在一处JNDI注入漏洞,攻击者在可以控制日志内容的情况下,通 ...
- 2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地。 请你找出一个海洋单元格,这个海洋单元格
2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地. 请你找出一个海洋单元格,这个海洋单元格 ...
- 密码加密处理MD5与Salt
作用:一般用来加密或者签名(校验和) 特点: MD5算法不可逆如何内容相同无论执行多少次md5生成结果始终是一致 生成结果:始终是一个16进制32位长度字符串 //使用MD5 + salt +hash ...