codeforces Arrival of the General 题解
2014-04-30 09:22
295 查看
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
output
input
output
相当于一个简单的冒泡排序了,不过不用直接排序,只是计算一下而已。
注意
1 最大值和最小值交换的时候可以减少一次交换的。
2 元素是会重复的。
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); }
相关文章推荐
- codeforces Arrival of the General 题解
- codeforces gym 2016-2017 NEERC, Moscow Subregional K. Knights of the Old Republic 最小生成树+dp
- the thinker of code complete -- general issues in using variables
- NOJ [1039] Arrival of the General
- Predict Outcome of the Game CodeForces - 451C (分情况讨论)
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
- Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数
- CodeForces April Fools Day Contest 2018 B-A Map of the Cat
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
- A. Arrival of the General
- HDU 4150 Powerful Incantation / Codeforces 625B - War of the Corporations
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
- Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
- Arrival of the General
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man
- Educational Codeforces Round 4-D. The Union of k-Segments
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- codeforces D. The Union of k-Segments 排序