手写模板:
![](http://img.e-com-net.com/image/info8/2b246dbcfe82488397e9aa432bbc4e31.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int maxn=100000+10; 10 int n; 11 int heap[maxn]; 12 void up(int p) { 13 while (p>1) { 14 if (heap[p]>heap[p/2]) { 15 swap(heap[p], heap[p/2]); 16 p/=2; 17 } 18 else break; 19 } 20 } 21 void insert(int val) { 22 heap[++n]=val; 23 up(n); 24 } 25 int top() { 26 return heap[1]; 27 } 28 void down(int p) { 29 int s=p*2; 30 while (s<=n) { 31 if (s 1]) s++; //左右子节点取较大者 32 if (heap[p]<heap[s]) { 33 swap(heap[p], heap[s]); 34 p=s; s=p*2; 35 } 36 else break; 37 } 38 } 39 void extract() { 40 heap[1]=heap[n--]; 41 down(1); 42 } 43 void remove(int k) { 44 heap[k]=heap[n--]; 45 up(k), down(k); 46 } 47 48 int main() { 49 //freopen("a.txt", "r", stdin); 50 //freopen("a.out", "w", stdout); 51 scanf("%d", &n); 52 for (int i=1; i<=n; ++i) { 53 int x; 54 scanf("%d", &x); 55 insert(x); 56 printf("%d\n", top()); 57 } 58 return 0; 59 }
STL:
#includepriority_queue<int> q;
【例题】POJ1456 Supermarket
![](http://img.e-com-net.com/image/info8/2b246dbcfe82488397e9aa432bbc4e31.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int maxn=100000+10; 10 int n; 11 struct node { 12 int p, d; 13 } a[maxn]; 14 priority_queue<int, vector<int>, greater<int> > Q; 15 16 bool cmp(node a, node b) { 17 return a.d<b.d; 18 } 19 20 int main() { 21 //freopen("a.txt", "r", stdin); 22 //freopen("a.out", "w", stdout); 23 while (~scanf("%d", &n)) { 24 for (int i=1; i<=n; ++i) 25 scanf("%d%d", &a[i].p, &a[i].d); 26 sort(a+1, a+n+1, cmp); 27 Q.push(a[1].p); 28 for (int i=2; i<=n; ++i) { 29 int w=Q.top(), sz=Q.size(); 30 if (a[i].d==sz && a[i].p>w) { 31 Q.pop(); 32 Q.push(a[i].p); 33 } 34 else if (a[i].d>sz) { 35 Q.push(a[i].p); 36 } 37 } 38 int ans=0; 39 while (!Q.empty()) { 40 ans+=Q.top(); 41 Q.pop(); 42 } 43 printf("%d\n", ans); 44 } 45 return 0; 46 }
【例题】POJ2442 Sequence
![](http://img.e-com-net.com/image/info8/2b246dbcfe82488397e9aa432bbc4e31.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int maxn=2000+10; 10 const int maxm=100+10; 11 int n, m; 12 int A[maxm][maxn], f[maxn]; 13 14 struct node { 15 int p1, p2, i, j; 16 bool flag; 17 node() {}; 18 node(int a, int b, int c, int d, int f) { 19 p1=a, p2=b, i=c, j=d, flag=f; 20 } 21 }; 22 bool operator < (const node& a, const node& b) { 23 return (a.i+a.j)>(b.i+b.j); 24 } 25 26 priority_queue Q; 27 28 void merge(int k) { 29 while (!Q.empty()) Q.pop(); 30 Q.push(node(1, 1, f[1], A[k][1], 0)); 31 int c[maxn], cnt=0; 32 while (!Q.empty()) { 33 node x=Q.top(); Q.pop(); 34 c[++cnt]=x.i+x.j; 35 if (x.p2 1, f[x.p1], A[k][x.p2+1], 1)); 36 if (!x.flag && x.p1 1, x.p2, f[x.p1+1], A[k][x.p2], 0)); 37 if (cnt==m) break; 38 } 39 for (int i=1; i<=m; ++i) f[i]=c[i]; 40 } 41 42 int main() { 43 //freopen("a.txt", "r", stdin); 44 //freopen("a.out", "w", stdout); 45 int T; 46 scanf("%d", &T); 47 while (T--) { 48 while (!Q.empty()) Q.pop(); 49 scanf("%d%d", &n, &m); 50 for (int i=1; i<=n; ++i) 51 for (int j=1; j<=m; ++j) 52 scanf("%d", &A[i][j]); 53 for (int i=1; i<=n; ++i) sort(A[i]+1, A[i]+m+1); 54 for (int i=1; i<=m; ++i) f[i]=A[1][i]; 55 for (int i=2; i<=n; ++i) merge(i); 56 for (int i=1; i<=m; ++i) printf("%d ", f[i]); 57 puts(""); 58 } 59 return 0; 60 }
Huffman树
构造一棵包含n个叶子节点的k叉树,其中第i个叶子节点带有权值Wi,要求最小化sigma(Wi*Li)。
【例题】CH1701/NOIP2004 合并果子
![](http://img.e-com-net.com/image/info8/2b246dbcfe82488397e9aa432bbc4e31.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int maxn=2000+10; 10 const int maxm=100+10; 11 int n; 12 priority_queue<int, vector<int>, greater<int> > Q; 13 14 int main() { 15 //freopen("a.txt", "r", stdin); 16 //freopen("a.out", "w", stdout); 17 scanf("%d", &n); 18 for (int i=1; i<=n; ++i) { 19 int x; 20 scanf("%d", &x); 21 Q.push(x); 22 } 23 int ans=0; 24 while (Q.size()!=1) { 25 int x=Q.top(); Q.pop(); 26 int y=Q.top(); Q.pop(); 27 ans+=x+y; 28 Q.push(x+y); 29 } 30 printf("%d\n", ans); 31 return 0; 32 }