题解 P4995 【跳跳!】
Fractures
2018-11-05 20:52:47
# 这么~~难~~的题怎么没有人用堆呢?!!
本蒟蒻就来用堆来写一下这个题
在这里,我的思路是:开一个大根堆和小根堆,把最大的的和最小的统计出来。 因为题上写了使耗费的体力最大,所以只要把最大值和最小值交替相减求平方就行了
##### 给大佬们展示一下我的做法:
```cpp
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
priority_queue<int>p;//大根堆
priority_queue<int,vector<int>,greater<int> >f;//小根堆
int n;
long long ans;//必须开long long!!!我就是这个比赛才得了50!!!
int tmp1,tmp2;
int a[301];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){//读入大小根堆
scanf("%d",&a[i]);
p.push(a[i]);
f.push(a[i]);
}
ans+=pow(p.top(),2);//从地面跳到最大的石头
for(int i=1;i<=n;i++){//交替相减
if(i%2!=0){//从大石头跳到小石头
tmp1=p.top();
p.pop();
tmp2=f.top();
}
if(i%2==0){//从小石头跳到大石头
tmp1=p.top();
tmp2=f.top();
f.pop();
}
ans+=pow(tmp1-tmp2,2);
}
printf("%lld",ans);
return 0;
}
```