P4995 跳跳!

//
// Created by Sween'e'y on 2022/9/27.
//
//P4995 跳跳!

#include <iostream>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <deque>
#include <array>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <climits>
#include <string>
#include <cstring>
#include <cctype>
#include <cassert>
#include <cstdbool>
#include <bitset>
#include <list>
#include <tuple>


using namespace std;
const double PI = acos(-1.0);
//浮点数比较
const double eps = 1e-8;
#define Equ(a, b) (fabs((a)-(b))<(eps))
#define More(a, b) ((a)-(b))>(eps)
#define Less(a, b) ((a)-(b))<(-eps)
#define MoreEqu(a, b) ((a)-(b))>(-eps)
#define LessEqu(a, b) ((a)-(b))<(eps)
const int INF = 0x3fffffff; //更常用
//memset(arr, 0x7f, sizeof arr); //浮点数的正无穷大,这个有用
//memset(arr, 0x3f, sizeof arr);//每个元素赋值成0x3f3f3f3f,即1061109567,通常看成int的无穷大
struct cmp {
    bool operator()(int a, int b) {
          return a > b;
    }
};

//自定义数据结构,优先队列
struct te {
    int x;

    te(int a) {
          x = a;
    };

    bool operator<(const te &a) const {
          return x < a.x;//大顶堆
    }
};

template<class T>
int length(T &array) {
      return sizeof(array) / sizeof(array[0]);
}

bool is_prime[100007];

void ai() {
      for (int i = 2; i <= 100000; i++) {
            is_prime[i] = 1;
      }
      for (int i = 2; i <= 100000; i++) {
            if (is_prime[i]) {
                  for (int j = 2 * i; j <= 100000; j += i) {
                        is_prime[j] = 0;
                  }
            }
      }
}

int numberInversion(int x) {
      int num = 0;
      while (x != 0) {
            num = num * 10 + x % 10;
            x /= 10;
      }
      return num;
}

double half(double x) {
      return x / 2;
}

string half(string s) {
      int n = s.length() / 2;
      char *str = new char[n];
      for (int i = 0; i < n; i++) {
            str[i] = s[i];
      }
      return str;
}

//字符串反转并删除前导0
string reverse(string s) {
      int cnt = 0;
      reverse(s.begin(), s.end());
      for (auto i: s) {
            if (i == '0') {
                  cnt++;
            } else {
                  break;
            }
      }
      s.erase(s.begin(), s.begin() + cnt);
      return (s != "" ? s : "0");
}

//删除后导零
string deleteTail(string s) {
      int cnt = 0;
      for (int i = s.length() - 1; i >= 0; i--) {
            if (s[i] == '0') {
                  cnt++;
            } else {
                  break;
            }
      }
      s.erase(s.end() - cnt, s.end());
      return (s != "" ? s : "0");
}

bool is_leap(int year) {
      return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

//组合
long long C(int n, int m) {
      if (m < n - m) {
            m = n - m;
      }
      long long ans = 1;
      for (int i = m + 1; i <= n; i++) {
            ans *= i;
      }
      for (int i = 1; i <= n - m; i++) {
            ans /= i;
      }
      return ans;
}

int arr[305];
long long ans;

int main() {
      //求每次的最大的高度差
      int n;
      scanf("%d", &n);
      for (int i = 1; i <= n; i++) {
            scanf("%d", &arr[i]);
      }
      sort(arr + 1, arr + n + 1);//排序
      int l = 1, r = n, flag = 0;
      ans += arr[n] * arr[n];   //跳到最高
      while (l <= r) {
            if (flag % 2 == 0) {  //从最高跳到第一个
                  ans += pow(arr[r] - arr[l], 2);
                  r--; //改变最高的,因为已经跳过了
            } else {//从第一个跳到 "最高的"
                  ans += pow(arr[l] - arr[r], 2);
                  l++;//同理
            }
            flag++;//改变跳跃方向
      }
      printf("%lld", ans);
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值