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 ...
随机推荐
- js做四则运算时,精度丢失问题及解决方法
一.前言:这个问题可以说是程序员必踩的坑,因此网上针对该问题的分析有很多也很详细,解决方法也比较统一,写法也是大同小异,本以为预期效果真能如他们所说是完美的,然而效果却是差强人意. 二.问题:首先,先 ...
- dev-c++ 使用教程
Dev C++ 支持单个源文件的编译,如果你的程序只有一个源文件(初学者基本都是在单个源文件下编写代码),那么不用创建项目,直接运行就可以:如果有多个源文件,才需要创建项目. 一.新建源文件 1.通过 ...
- 如何实现图像搜索,文搜图,图搜图,CLIP+faiss向量数据库实现图像高效搜索
如何实现图像搜索,文搜图,图搜图,CLIP+faiss向量数据库实现图像高效搜索 这是AIGC的时代,各种GPT大模型生成文本,还有多模态图文并茂大模型, 以及stable diffusion和sta ...
- 循环依赖导致编译或者服务启动报错问题:The dependencies of some of the beans in the application context form a cycle
错误如图: 我的是服务器启动服务时报错: ***************************APPLICATION FAILED TO START************************* ...
- vue-test --------模板引用
<template> <input type="text" v-model.lazy="message"> <div>{{m ...
- Mongodb安装篇+可视化工具篇
下载MongoDB 官网下载地址:Download MongoDB Community Server | MongoDB Version 选择:稳定版4.4.2 Mongo的版本分为稳定版和开发版 ...
- Java8新特性Stream流
1.是什么? Stream(流)是一个来自数据源的元素队列并支持聚合操作 2.能干嘛? Stream流的元素是特定类型的对象,形成一个队列. Java中的Stream并不会存储元素,而是按需计算. 数 ...
- 如何使用sharding-sphere完成读写分离和分库分表?
一.sharding-sphere配置读写分离 1.先搭建好一个MySQL的主从集群,可以参考[MySQL主从集群搭建] 2.在项目中导入相关依赖(记得刷新Maven) <!--读写分离--&g ...
- 为什么要重写equals()?
为什么要重写equals()? Equals和 == 的区别: ==:是个运算符, 判断是否相等,基本数据类型进行判断 也可判断两个对象相等,比较两个对象的哈希码值 Equals:是个Object类的 ...
- 【GIT-精讲】从零玩转Git-基础理论
关于版本控制 一.什么是版本控制 版本控制(Version Control Systems)版本控制(Revision control)是一种软件工程技巧 在开发的过程中,确保由不同人所编辑的同一档案 ...