洛谷 P4995 跳跳!(贪心)

根据题意猜测是贪心,每次都跳距离差最大的石头,排个序左右横跳即可...
不开long long 见祖宗
AcCode
#include<bits/stdc++.h>
using namespace std;
int h[310];
int main(){
int n;
long long res = 0; //用int过不去
cin >> n;
for(int i = 0; i < n; i++){
cin >> h[i];
}
sort(h, h + n);
int bidx = n - 1, fidx = 0, pos = 0, cnt = 0;
while(cnt < n){
if(cnt % 2 == 0){
res += (h[bidx] - pos) * (h[bidx] - pos);
pos = h[bidx];
bidx--;
}else{
res += (h[fidx] - pos) * (h[fidx] - pos);
pos = h[fidx];
fidx++;
}
cnt++;
}
cout << res;
return 0;
}

浙公网安备 33010602011771号