[P4995]跳跳!(贪心)

mac2022-06-30  22

这应该是我做过的最简单的洛谷月赛了

题意

给你n个高度,你的初始高度是0,现在要求你遍历每一个高度,每一次遍历耗费(hi−hj)2 的值

现在要你求耗费值最大

思路

真的是水……

排序一下,求一下就好。

注意下就是每次求时候右端点先-- 再左端点++

代码

#include<cstdio> #include<algorithm> using namespace std; #define N 305 #define ll long long int n; int h[N]; ll ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); sort(h + 1, h + 1 + n); ans = h[n] * h[n]; int l = 1, r = n; while (l < r) { ans += (h[r] - h[l])*(h[r] - h[l]); r--; ans += (h[r] - h[l])*(h[r] - h[l]); l++; } printf("%lld", ans); return 0; }

 

转载于:https://www.cnblogs.com/lincold/p/9903718.html

最新回复(0)