codeforces 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
2
7
10 10 58 31 63 40 76
10
相当于一个简单的冒泡排序了,只是不用直接排序,仅仅是计算一下而已。
注意
1 最大值和最小值交换的时候能够降低一次交换的。
2 元素是会反复的。
#include <iostream>
using namespace std; namespace{
static const int MAX_VAL = (int) 1E9;
static const int MIN_VAL = (int) 1E-9;
}
void ArrivaloftheGeneral()
{
int n, max_i, min_i, max_n = MIN_VAL, min_n = MAX_VAL, a;
cin>>n;
for (unsigned i = 0; i < n; i++)
{
cin>>a;
if (a <= min_n)
{
min_n = a;
min_i = i;
}
if (a > max_n)
{
max_n = a;
max_i = i;
}
}
cout<<max_i + (n - min_i - 1) - (max_i > min_i);
}
codeforces Arrival of the General 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- javascript高级特性(面向对象)
javascript高级特性(面向对象): * 面向对象: * 面向对象和面向过程的区别: * 面向对象:人就是对象,年龄\性别就是属性,出生\上学\结婚就是方法. * 面向过程:人出生.上学.工作. ...
- css动画+滚动的+飞舞的小球
源代码如下: <!DOCTYPE html><html><head> <title>xi</title> <meta charset= ...
- c - 给分数分级别
/* 题目: 学习成绩>=90 分的同学用 A 表示, 80-89 分之间的用 B 表示,70-79 分的用 C 表示, 60-69 分用 D表示,小于60分用E表示. 分析: 使用swith. ...
- 在vim中设置 '打印时间'的快捷键.
在 ~/.vimrc (没有该文件可以手动创建)中输入 map <F4> <Esc>:r !date<CR> 实现在 '一般模式'状态点击 F4时,自动在vim中打 ...
- iOS9.3 Could not find Developer Disk Image 真机调试错误解决办法
昨天手机系统升级9.3,到了公司真机测试的时候发现Xcode(7.2.1)报错:Could not find Developer Disk Image , 这是因为我的Xcode7.2中还没有支持9. ...
- CentOS 6.5上安装Python 2.7.9
CentOS 6.6自带的是Python 2.6.6,而编译llvm需要Python 2.7以上. checking for python... /usr/bin/python checking fo ...
- php 防止SQL注入函数
function inject_check($sql_str) { return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/ ...
- 成功启动了Apache却没有启动apache服务器
原因没有用管理员身份运行...
- 获取MP3和M4A音乐文件的歌曲信息以及专辑图片--备用
NSBundle* bundle = [NSBundle mainBundle]; NSString* path = [bundle bundlePath]; NSURL * file ...
- RMAN-06023: no backup or copy of datafile 6 found to restore
一:问题描述 我用指定备份集恢复时,报错: RMAN> run { 2> shutdown immediate; 3> startup mount; 4> allocate c ...