NOJ [1039] 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.

  • 输入
  • 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.
  • 输出
  • Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
  • 样例输入
  • 4
    33 44 11 22
    7
    10 10 58 31 63 40 76
    
  • 样例输出
  • 2
    10
    
题目说了一大堆,只有那么几句话有用,就是把最小值放到最右边,最大值放到最左边,然后每次只能交换相邻的数

分析:先找出最大小值的下标,注意,可能有多个最大值(最小值),对于最大值,取最左边的,对于最小值,取最右边的,
然后,如果最大值在最小值左边,直接算,在右边的话,由于定有一步是最大值和最小值交换,所以计算完后要-1
#include<stdio.h>
#include<string.h>

int main()
{
	int n;
	int temp,max,min,locate_max,locate_min;
	while(~scanf("%d",&n))
	{
		max=-500;
		min=500;
		locate_max=1; //初始化 
		locate_min=n;
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			if(temp>max)
			{
				max=temp;
				locate_max=i;
			}
			if(temp<=min)
			{
				min=temp;
				locate_min=i;
			}
		}
		if(locate_max < locate_min)
			cnt+=locate_max-1+n-locate_min;
		else
		    cnt+=(n-locate_min)+(locate_max-1)-1;
		printf("%d\n",cnt);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值