bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板

时间:2023-03-09 03:05:44
bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,a;
typedef long long ll;
ll ans;
void read(int &x){
x=; int f=; char ch;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') f=-;
for (;isdigit(ch);ch=getchar()) x=x*+ch-''; x*=f;
}
priority_queue< int,vector<int>,greater<int> >heap;
int main(){
read(n),ans=;
for (int i=;i<=n;i++) read(a),heap.push(a);
for (int x,y,i=;i<n;i++){
x=heap.top(),heap.pop();
y=heap.top(),heap.pop();
ans+=x+y; heap.push(x+y);
}
printf("%lld\n",ans);
return ;
}

做法:堆入门题,学习系统堆的用法——>雾(:

大根堆:priority_queue<int>heap;

小根堆:priority_queue< int,vector<int>,greater<int> >heap;

heap.top() 函数:堆顶的元素值;

heap.pop()过程:弹出堆顶元素;

heap.empty()函数:堆空与否,堆空则返回1,否则返回0;

heap.push()过程:在堆中加入一个元素,并维护堆的性质。