A. Stone Game
代码
点击查看代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int t;
cin >> t;
while(t -- ){
int n;
cin >> n;
vector<int> v(n);
for(int &e:v)cin >> e;
int maxp = max_element(v.begin(),v.end()) - v.begin();
int minp = min_element(v.begin(), v.end()) - v.begin();
cout << min({
max(maxp, minp) + 1,
(n - 1) - min(maxp, minp) + 1,
(n - 1) - maxp + minp + 2,
(n - 1) - minp + maxp + 2
}) << '\n';
}
}
收获
- min函数用中括号可以对多个数求最小值,不过要加头文件algorithm
- max_element返回最大元素的地址
- min_element返回最小元素的地址
- vector
v(n + 1)可以实现从1开始输入