A. Arrival of the General

题目链接:http://codeforces.com/problemset/problem/144/A

144A. Arrival of the General

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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
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:
1.(10, 10, 58, 31, 63, 40, 76)
2.(10, 58, 10, 31, 63, 40, 76)
3.(10, 58, 10, 31, 63, 76, 40)
4.(10, 58, 10, 31, 76, 63, 40)
5.(10, 58, 31, 10, 76, 63, 40)
6.(10, 58, 31, 76, 10, 63, 40)
7.(10, 58, 31, 76, 63, 10, 40)
8.(10, 58, 76, 31, 63, 10, 40)
9.(10, 76, 58, 31, 63, 10, 40)
10.(76, 10, 58, 31, 63, 10, 40)
11.(76, 10, 58, 31, 63, 40, 10)

代码

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	int n,i;
	int max,min;
	int tmax,tmin;
	int sum;
	int a[102];
	scanf("%d",&n);
	max=0;
	min=101;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]<=min)
		{
			min=a[i];
			tmin=i;
		}
		if(a[i]>max)
		{
			max=a[i];
			tmax=i;
		}
	}
	if(tmin>=tmax)
	{
		sum=n-tmin-1+tmax;//数组下标从0开始,最大值与最小值相邻时进行交换只需要一步,所以+1;
	}
	else
	{
		sum=n-tmin-1+tmax-1;//数组下标从0开始
	}
	printf("%d\n",sum);


	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
操作系统代码实现:Number Project Name Content Summary State Type 一、Process Scheduling Algorithm Simulation 1、 Simulate the operation of the round-robin algorithm for process scheduling. 2、 Create at least 15 processes and output their scheduling situation under the scheduling algorithm mentioned above and output it to theterminal to check the execution of the algorithm. 3、 The output should include the arrival time of the processes, the end time, and the average execution time. Essential. General 二、Readers-Writer Problem Implmentation 1、 A data set is shared among several concurrent processes: Readers – only read the data set; they do not perform any updates. Writers – can both read and write. 2、 Problem – allow multiple readers (at most 8) to read at the same time. Only one single writer can access the shared data at the same time. Essential. General 三、Program for Least Recently used Algorithm 1、 Create a page access sequence (page number range 0-18) using a random function. The sequence length is 54 and assume that the number of main memory frames allocated to the thread is 6, that is, M = 6. 2、 Implement the LRU algorithm for page replacement on the above access sequence. 3、 Output the page replacement sequence and the page fault rate. Essential. General Requirements 1、 For each experiment project, submit a design report and code. The code should be implemented in C++. The requirements are as follows: a) The content of the design report should include the design ideas and implementation. b) The results of the design report should include testing and running results (screenshots of screen outputs). c) The conclusion of the design report should summarize the problems encountered, the solutions and experiences during the implementation process.
最新发布
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稚皓君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值