因为两个石头高度差距越大,体力消耗越多
所以先从地面跳到最高的石头上,然后跳到最低的石头上
然后再跳到次高的石头上以此类推直到跳完
#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int rock_height[N];
int rock_amount;
int cost(int height_a, int height_b)
{
return abs(height_a - height_b) * abs(height_a - height_b);
}
int main()
{
cin >> rock_amount;
for (int i = 0; i < rock_amount; i++)
{
cin >> rock_height[i];
}
sort(rock_height, rock_height + rock_amount, greater<int>());
long long j = rock_amount - 1;
long long rock_count = 0;
long long cost_sum = 0;
long long last_height = 0;
for (int i = 0; i < rock_amount;)
{
if (rock_count == rock_amount)
{
break;
}
rock_count++;
if (rock_count % 2 == 1)
{
if (i == 0)
{
cost_sum += cost(rock_height[i], 0);
last_height = rock_height[i];
i++;
}
else
{
cost_sum += cost(rock_height[i], last_height);
i++;
}
}
if (rock_count % 2 == 0)
{
cost_sum += cost(rock_height[j], last_height);
last_height = rock_height[j];
j--;
}
}
cout << cost_sum << endl;
return 0;
}