1.20第一题

A - 1

Time limit 2000 ms
Memory limit 262144 kB

Problem Description

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 Input

4
33 44 11 22

7
10 10 58 31 63 40 76

Sample Output

2

10

问题链接:A - 1

问题简述:

输入n代表所含数的个数,接下来输入n个数,将这n个数最大值和最小值通过相邻数交换的形式分别放到第一位和最后一位,问需要多少步才能达到目的?

问题分析:

有点像冒泡排序但是只需要计算,需要注意的是当最大值和最小值交换的时候步数是公用一次所以要注意-1,最大值或最小值可以有重复数。
一种做法:
①找出最大最小值
②从左往右扫找到最大值然后计算放到第一位的步数并执行交换指令
③从右往左扫找到最小值然后计算放到最后一位的步数
④两次步数的和即为答案

程序说明:

a[200]用于放最初的输入值,b[200]是a[200]的备份用于排序找出最大最小值以及对应在数组的位置,temp用于交换值时充当交换中介,number用于记录最大最小值在数组的位置(用一次记录一次,无需特意保存下来所以只定义一个number),sum=0用于记录步数,min、max用于记录最小值最大值,sort函数用于对b[200]进行排序。

AC通过的C语言程序如下:

#include <iostream>
#include<algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int a[200];
	int b[200];
	int temp;
	int number;
	int sum = 0;
	int min;
	int max;
	for (int i = 0;i < n;i++)
	{
		cin >> a[i];
	}
	for (int i = 0;i < n;i++)
	{
		b[i]= a[i];
	}
	sort(b, b + n);
	min = b[0];
	max = b[n-1];
	for (int i = 0;i < n;i++)
	{
		if (a[i] == max)
		{
			number = i;
			break;
		}
	}
	for (int i = number;i != 0;i--)
	{
		temp = a[i];
		a[i] = a[i - 1];
		a[i - 1] = temp;
		sum++;
	}	
	for (int i = n-1;i != n;i--)
	{
		if (a[i] == min)
		{
			number = i;
			break;
		}
	}
	for (int i = number;i != n-1;i++)
	{
		temp = a[i];
		a[i] = a[i + 1];
		a[i + 1] = temp;
		sum++;
	}
	cout << sum;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86562119