您的位置:首页 > 其它

CodeForces 144A Arrival of the General

2013-08-16 15:24 211 查看
A. Arrival of the General

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 alln 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 integersa1, 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.
Numbersa1, 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


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)

##################################################################################################################################3==========我是分割线============################################################################################################################33333

题意其实很简单,就是把这个数列中最大的数放在最前面,最小的数放后面。

求的是逐步交换的步数。。也就是规定一次只能交换相邻位置的数。

数据规模小,直接用计数器counter去数步数就行。。

另一种方法就是找出最靠前的最大元素和最靠后的最小元素。判断它们的相对位置关系,A.大者靠前:只需要分别减出步数再加和;

B:小者靠前:就要先算出来这两个元素交换完成时走了多少步,再重复A,加和。

放上第一种的代码吧。。

#include <iostream>
using namespace std;

int main(){
int a[100];
for (int i = 0; i < 100; ++i)
{
a[i]=0;
}
int n;
cin>>n;
int largest=0;
int large_loc=0;
int littlest=111;
int little_loc=0;
for (int i = 0; i < n; ++i)
{
cin>> a[i];
if (a[i]>largest)
{
largest=a[i];
large_loc=i;
}
if (a[i]<=littlest)
{
littlest=a[i];
}
}
int counter=0;
while(large_loc!=0){
int temp;
temp=a[large_loc];
a[large_loc]=a[large_loc-1];
a[large_loc-1]=temp;
large_loc--;
counter++;
}
for (int i = 0; i < n; ++i)
{
if (a[i]<=littlest)
{
littlest=a[i];
little_loc=i;
}
}
while(little_loc!=n-1){
int temp;
temp=a[little_loc];
a[little_loc]=a[little_loc+1];
a[little_loc+1]=temp;
little_loc++;
counter++;
}
cout<<counter<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: