#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class solution
{
public:
int max_v(vector<int> stu)
{
vector<int> newl(2*(stu.size()/2),0);
size_t num = stu.size();
int times = num / 2;
bool flag = 1;
int re=0;
for (int i = 0; i < times; i++)
{
vector<int>::iterator max = max_element(stu.begin(), stu.end());
int maxv = *max;//注意不能讲两个erase写在一起,因为erase一个后向量会将原来的位置的元素擦除掉,所有它下面的数据上移,因此此时min地址就不在正确
stu.erase(max);//根据地址
vector<int>::iterator min = min_element(stu.begin(), stu.end());
int minv = *min;
stu.erase(min);
if (flag == 1)//利用flag符号标记,翻转放置形式的放置数据
{
newl[times - 1 + i+1] = minv;
}
else
{
//newl.insert(newl.begin() + times - 1 - i, minv);
newl[times - 1 - i]=minv;
//newl.insert(newl.begin() + times - 1 + i + 1, maxv);
newl[times - 1 + i + 1] = maxv;
}
flag = !flag;//取反用的是!,而~似乎在c++不是特定的运算符
}
for (int i = 0; i < newl.size()-1; i++)
{
re += abs(newl[i + 1] - newl[i]);
}
if (num % 2 == 1)
{
int re1 = re + abs(newl[newl.size() - 1] - stu[0]);
int re2 = re + abs(newl[0] - stu[0]);
if (re1>re2)
re = re1;
else
re = re2;
}
return re;
}
};
int main()
{
int n;
solution s;
cin >> n;
vector<int> stu;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
stu.push_back(temp);
}
int max=s.max_v(stu);
cout << max;
return 0;
}
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class solution
{
public:
int max_v(vector<int> stu)
{
vector<int> newl(2*(stu.size()/2),0);
size_t num = stu.size();
int times = num / 2;
bool flag = 1;
int re=0;
for (int i = 0; i < times; i++)
{
vector<int>::iterator max = max_element(stu.begin(), stu.end());
int maxv = *max;//注意不能讲两个erase写在一起,因为erase一个后向量会将原来的位置的元素擦除掉,所有它下面的数据上移,因此此时min地址就不在正确
stu.erase(max);//根据地址
vector<int>::iterator min = min_element(stu.begin(), stu.end());
int minv = *min;
stu.erase(min);
if (flag == 1)//利用flag符号标记,翻转放置形式的放置数据
{
//newl.insert(newl.begin() + times - 1 - i, maxv);//插入是在原有向量基础上插入新的元素,此时向量会扩大,不适用此处,此处实现的是在原有向量特定
位置处赋予新的值,此时用数组索引方法赋值,如下句
newl[times - 1 -i] = maxv;
//newl.insert(newl.begin() + times - 1 + i + 1, minv);newl[times - 1 + i+1] = minv;
}
else
{
//newl.insert(newl.begin() + times - 1 - i, minv);
newl[times - 1 - i]=minv;
//newl.insert(newl.begin() + times - 1 + i + 1, maxv);
newl[times - 1 + i + 1] = maxv;
}
flag = !flag;//取反用的是!,而~似乎在c++不是特定的运算符
}
for (int i = 0; i < newl.size()-1; i++)
{
re += abs(newl[i + 1] - newl[i]);
}
if (num % 2 == 1)
{
int re1 = re + abs(newl[newl.size() - 1] - stu[0]);
int re2 = re + abs(newl[0] - stu[0]);
if (re1>re2)
re = re1;
else
re = re2;
}
return re;
}
};
int main()
{
int n;
solution s;
cin >> n;
vector<int> stu;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
stu.push_back(temp);
}
int max=s.max_v(stu);
cout << max;
return 0;
}