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 题解 直接 ...
随机推荐
- json数组传递到后台controller
现前台有如下格式的数据需要传递到后台的controller, public class UpdatePara { public int RoleID { get; set; } public List ...
- iOS8中添加的extensions总结(二)——分享扩展
分享扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutorials> 1.准备 这次例子来源于国外的图片分享网站Imgur.com 首 ...
- PHP Date/Time 函数
Runtime 配置 Date/Time 函数的行为受到 php.ini 中设置的影响: 名称 描述 默认 PHP 版本 date.timezone 规定默认时区(所有的 Date/Time 函数使用 ...
- jQuery get/post区别及contentType取值
1.GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配]所以 第二次访问的时候 如果 URL字符串没变化浏览器是直接拿出了第一次访问的结果,表 ...
- cocos2d-x 工程目录结构说明
下载最新的cocos2d-x,解压完之后呈现出如下的目录结构: cocos2dx:框架核心目录,里面是存放了2dx引擎的核心代码. CocosDenshion:各个平台的音效实现. document: ...
- OpenStack虚机相关错误
OpenStack配置起来还是挺麻烦的,特别是网络那块.虽然官方文档越来越清晰,但有时还是会出各种错.排错主要是看日志.看官方文档和google 以下就一些虚机相关常见的错误做一下总结(基于Iceho ...
- C程序设计语言练习题1-20
练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方.假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位.n应该是变量还是符号 ...
- NET MVC+EF6+Bootstrap
开源:ASP.NET MVC+EF6+Bootstrap开发框架 前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿1 ...
- 安卓4.2原生rom状态栏显示运营商
前言:要调整状态栏布局,需反编译systemui.apk.单卡机修改status_bar.xml和signal_cluster_view.xml,双卡机修改gemini_status_bar.xml和 ...
- 使用dom4j生成xml字符串,以及解析xml字符串
基于dom4j-1.6.1.jar import java.io.IOException; import java.io.StringWriter; import java.util.ArrayLis ...