正经题解| 前行的拦截
2024-04-01 10:42:13
发布于:浙江
31阅读
0回复
0点赞
题目解析
我们需要先找到序列中的最大值与最小值的位置,即下标,但是在一个序列中,可以会出现多个最大值与多个最小值,最大值应离头
越近越好,最小值应离尾
越近越好。
这里还需要注意的就是当最大值的位置在最小值的右边时,需要多交换一次。
AC代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int t,n,m;
int main() {
ios::sync_with_stdio(false);
int _maxi = 0,_mini = 0;
int _max = 0,_min = 100;
int x;
cin >> n;
for(int i=0;i<n;i++) {
cin >> x;
if (x > _max) {
_max = x;
_maxi = i;
}
if (x <= _min) {
_min = x;
_mini = i;
}
}
int ans = (_maxi + (n - 1 - _mini));
if (_maxi > _mini)ans--;
cout << ans << endl;
return 0;
}
复杂度分析
遍历所有值就能找到最大值与最小值了,复杂度为 。
这里空空如也
有帮助,赞一个