C - Arrival of the General
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.
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:
- (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)
解题思路:题目的意思就是规定正确的序列中最大值在第一个位置,最小值在最后一个位置。要求将不满足序列中的元素采用最小交换次数将其变成正确的序列。做法:先找出最小值的下标k,注意a[k]>=a[i](等号要加上),再将k后面的元素往前移一位,再去找最大值的下标即可,水过!
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m=,k=,a[];
cin>>n;
for(int i=;i<n;++i)cin>>a[i];
for(int i=;i<n;++i)
if(a[k]>=a[i])k=i;//先找出最小值的下标
m+=n-k-;
for(int i=k;i<n-;++i)a[i]=a[i+];
k=;
for(int i=;i<n-;++i)
if(a[k]<a[i])k=i;//找最大值的下标
m+=k;
cout<<m<<endl;
return ;
}
C - Arrival of the General的更多相关文章
- codeforces Arrival of the General 题解
		A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ... 
- mysql general log日志
		注:应一直出现http://www.cnblogs.com/hwaggLee/p/6030765.html文章中的问题 故mysql general log日志.查看具体是什么命令导致的. 打开 ge ... 
- Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”
		Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为"通用职责分配软件模式" 1. GRA ... 
- OpenCASCADE General Transformation
		OpenCASCADE General Transformation eryar@163.com Abstract. OpenCASCADE provides a general transforma ... 
- RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)
		前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ... 
- 地理信息系统 - ArcGIS - 高/低聚类分析工具(High/Low Clustering ---Getis-Ord General G)
		前段时间在学习空间统计相关的知识,于是把ArcGIS里Spatial Statistics工具箱里的工具好好研究了一遍,同时也整理了一些笔记上传分享.这一篇先聊一些基础概念,工具介绍篇随后上传. 空间 ... 
- java-collections.sort异常Comparison method violates its general contract!
		转载:http://www.tuicool.com/articles/MZreyuv 异常信息 java.lang.IllegalArgumentException: Comparison metho ... 
- compass General 常用api学习[Sass和compass学习笔记]
		compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ... 
- c++错误——intermediate.manifest : general error c1010070很傻的错
		.\Debug\sadf.exe.intermediate.manifest : general error c1010070: Failed to load and parse the manife ... 
随机推荐
- Linux快速入门教程-进程管理ipcs命令学习
			使用Linux系统必备的技能之一就是Linux进程管理,系统运行的过程正是无数进程在运行的过程.这些进程的运行需要占用系统的内存等资源,做好系统进程的管理,对于我们合理分配.使用系统资源有非常大的意义 ... 
- modelsim 仿真软件 百度云分享 modelsim se 10.7 10.6d  10.6c  10.5 10.4
			modelsim se 10.7 链接:https://pan.baidu.com/s/1NDC2yMCZmA4bIRSk2dUiTg 提取码:4l1d 复制这段内容后打开百度网盘手机App,操作更方 ... 
- 强大而优雅,API 研发管理 EOLINKER 新版正式发布!
			EOLINKER 于2019年3月3日正式发布新版本!该版本大幅强化各个产品的功能.着重优化了全站的用户交互体验,并且EOLINKER AMS 产品正式更名为 EOLINKER API Studio ... 
- Django - 模版语言循环字典
			1.可以对传入字典参数做循环显示 views.py中代码: urls.py中代码: html中代码: 在模版语言中,可以对字典进行类似python中的操作(keys,values,items),需要注 ... 
- Python基础-判断类型统计个数
			写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 首先我们定义四个变量,分别存储数字.字母.空格.其他的数量 digital_temp = 0 # 存储数字个数 letter ... 
- BZOJ 1602 USACO 2008 Oct. 牧场行走
			[题解] 要求出树上两点间的距离,树上的边有边权,本来应该是个LCA. 看他数据小,Xjb水过去了...其实也算是LCA吧,一个O(n)的LCA... #include<cstdio> # ... 
- 从NLP任务中文本向量的降维问题,引出LSH(Locality Sensitive Hash 局部敏感哈希)算法及其思想的讨论
			1. 引言 - 近似近邻搜索被提出所在的时代背景和挑战 0x1:从NN(Neighbor Search)说起 ANN的前身技术是NN(Neighbor Search),简单地说,最近邻检索就是根据数据 ... 
- DOMContentLoaded 与onload区别以及使用
			一.何时触发这两个事件? 1.当 onload 事件触发时,页面上所有的DOM,样式表,脚本,图片,flash都已经加载完成了. 2.当 DOMContentLoaded 事件触发时,仅当DOM加载完 ... 
- Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression
			题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判 ... 
- R语言 PCA
			1.关键点 综述:主成分分析 因子分析 典型相关分析,三种方法的共同点主要是用来对数据降维处理的从数据中提取某些公共部分,然后对这些公共部分进行分析和处理. #主成分分析 是将多指标化为少数几个综合指 ... 
