Codeforces 144A 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.
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
Copy
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)
题意
为数据排序,移动最大值到最前方,最小值到最后一位
输入的时候不断比较,但在出现等于最小值时更新位置(让定位更接近最后一位)
当最大值在最小值后时,总移动一次数 - 1
#include<bits/stdc++.h>
using namespace std;
int n, i, p, q = 99, x, a, b;
int main() {
for (cin >> n; i++ < n;)
cin >> x, x > p ? p = x, b = i : 0, x <= q ? q = x, a = i : 0;
cout << b + n - a - 1 - (b > a);//判断最大值是否在最小值后
}
Codeforces 144A 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 ...
- Educational Codeforces Round 7 B. The Time 水题
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- C - Arrival of the General
Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...
- codeforces 712B B. Memory and Trident(水题)
题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...
- codeforces 712A A. Memory and Crow(水题)
题目链接: A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- codeforces 711A A. Bus to Udayland(水题)
题目链接: A. Bus to Udayland 题意: 找一对空位坐下来,水; 思路: AC代码: #include <iostream> #include <cstdio> ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- Keepalived+Nginx高可用案例(抢占式与非抢占式)
(1)下载安装Keepalived源码包 Keepalived官网源码包下载地址 在服务器上解压 tar -xf keepalived-2.2.8.tar.gz 安装相关前置依赖 yum -y ins ...
- 在路上---学习篇(一)Python 数据结构和算法 (6)基于GA(遗传)算法的小案例
独白 最近了解到一种算法叫遗传算法,对其比较感兴趣,研究了一下,是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法.遗传算法是从代表问题可 ...
- idea配置servlet项目找不到servlet jar包爆红【解决办法】
1.看你的implements 后面的Servlet是否大写了 2.大部分原因就是缺少servlet-api jar包或者idea找不到jar包 如果你是爆红的,那么问题就在这里,点击-号,重新添加这 ...
- Go笔记(5)-String详解
String详解 Go中的字符串是一个字节的切片,可以通过将其内容封装在双引号中来创建字符串,Go中的字符串Unicode兼容的,并且是UTF-8编码,字符串是一些字节的集合 (1)创建字符串 str ...
- 从0到1,手把手带你开发截图工具ScreenCap------001实现基本的截图功能
ScreenCap---Version:001 说明 从0到1,手把手带你开发windows端的截屏软件ScreenCap 当前版本:ScreenCap---001 支持全屏截图 支持鼠标拖动截图区域 ...
- [THUPC 2023 初赛] 背包
题目描述 本题中,你需要解决完全背包问题. 有 \(n\) 种物品,第 \(i\) 种物品单个体积为 \(v_i\).价值为 \(c_i\). \(q\) 次询问,每次给出背包的容积 \(V\),你需 ...
- [ABC246F] typewriter
Problem Statement We have a typewriter with $N$ rows. The keys in the $i$-th row from the top can ty ...
- 本地部署modelscope-agent
本地部署modelscope-agent 部署流程 在modelscope社区创建一个自己的空间(假设name是LocalAgent),clone空间到本地(或云服务器如魔搭Notebook) git ...
- SpringBoot测试用例的一些小技巧~
场景一:不想因为测试而对数据库产生脏数据 @Test public void testInsert() { User user = new User(); user.setUsername(" ...
- Kafka 的基本使用
Kafka 是一款分布式消息发布和订阅系统,最初的目的是作为一个日志提交系统来使用.现在,也可以作为一般的消息中间件来使用. 组件介绍 相关的组件介绍如下表所示: 组件 解释 Broker 实际 Ka ...