洛谷P4995跳跳 题解

题目传送门

让我们来看看写着AK的电脑是怎么解决的

来回跳,设所有石头排序之后高度是$h_1,h_2…h_{n-1}$,就按照$0->h_n->h_2->h_{n-1}->h_2…$来跳

不会一行代码解决排序的同学戳戳这里:STL排序

就这?

就这。

等等忘记贴代码了

#include<iostream>
#include<algorithm>
using namespace std;
int h[304];
inline int sq(int x){return x*x;}
int main(){
	int n;long long ans=0;cin>>n;
	for(int i=1;i<=n;i++)cin>>h[i];
	sort(h,h+n+1);
	int l=0,r=n;
	while(l<r){
		ans+=sq(h[r]-h[l]);
		l++;
		ans+=sq(h[r]-h[l]);
		r--;
	}
	cout<<ans<<endl;
}