蒟蒻求助呜呜呜,样例全过测试点就过了一个,内有思路详解
  • 板块P4995 跳跳!
  • 楼主Otion
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/12/19 12:59
  • 上次更新2023/10/24 07:13:32
查看原帖
蒟蒻求助呜呜呜,样例全过测试点就过了一个,内有思路详解
843596
Otion楼主2022/12/19 12:59

因为两个石头高度差距越大,体力消耗越多

所以先从地面跳到最高的石头上,然后跳到最低的石头上

然后再跳到次高的石头上以此类推直到跳完

#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;
}
2022/12/19 12:59
加载中...