You can use the priority queue for the question of "Uva 11136-Hoax or what". Here we will discuss how to use the minimum-maximum heap.
Question requirements: You need to find the minimum and maximum values, and delete the minimum and maximum values.
This requires a data structure that includes the following operations:
- Insert an element
- Get_min () gets the minimum element and deletes it from the set.
- Get_max () to the maximum element, and delete it from the set
If you only need to find the minimum or maximum value, you can use heap or a priority queue. But here we need to find the minimum and maximum values. We can still use heap, that is, Double-Ended heap/minimum-maximum heap/Double-Ended.
Heap.
The query efficiency is constant, and the deletion efficiency is O (log (n ))
The following is attached to the previous paper: http://pan.baidu.com/share/link? Consumer id = 1020027175 & uk = 70243437
The minimum-maximum heap is to maintain the properties of the two heaps (minimum heap and maximum heap) in a binary tree at the same time. The minimum heap at the beginning-the maximum heap is the minimum heap and the maximum heap appear alternately at a time based on the binary tree depth, but here the minimum-maximum heap is the minimum heap for the left subtree and the maximum heap for the right subtree, and the left brother cannot be greater than the right brother.
This article only describes how to insert elements, because deleting the maximum and minimum elements is similar to normal heap operations. You can easily write code by taking full advantage of the minimum-maximum heap limit in the delete operation. The following code does not know whether it is correct or not, because the submission error is displayed when the request is submitted on the Apsara stack Management Console. If you have changed the numbers, please explain.
/* The implementation of the minimum-maximum heap continuously maintains such a binary tree: The left subtree is the minimum heap, And the right subtree is the maximum heap. There are three operations: insert and insert at the same time maintain the minimum-the maximum heap property returns the maximum value returned at the same time delete the maximum value, and maintain the minimum-the maximum heap property returns the minimum value returned at the same time delete the minimum value, and maintain the minimum-maximum heap properties */# include <stdio. h> # include <string. h ># define maxn 100100 struct heap {int len; int a [maxn]; void init () {len = 0; memset (a, 0, sizeof (0 ));} void heap_insert (int x) {int p = ++ len; int gf; int l, r; while (1) {if (! (P & 1) & a P-1]> x) {a [p] = a P-1]; p --;} gf = (p + 1)> 2)-1; if (gf <0) break; if (gf = 0) l = 1, r = 2; else l = (gf <1) + 1, r = l + 1; if (x <a [l]) a [p] = a [l], p = l; else if (x> a [r]) a [p] = a [r], p = r; else break;} a [p] = x;} int get_min () {int p = 1; int Min = a [p]; int temp; a [p] = a [len --]; while (1) {if (p & 1) & p + 1 <= len & a [p]> a [p + 1]) {temp = a [p + 1]; a [p + 1] = a [p]; a [p] = temp;} Int lchild = (p <1) + 1; if (lchild> len) break; if (a [p]> a [lchild]) {temp = a [p]; a [p] = a [lchild]; a [lchild] = temp;} else break;} return Min;} int get_max () {int p = 2; int Max = a [p]; int temp; a [p] = a [len --]; while (1) {if (! (P & 1) & p-1> = 1 & a [p] <a [P-1]) {temp = a [p]; a [p] = a P-1]; a P-1] = a [p];} int rchild = (p <1) + 2; if (rchild> len) break; if (a [p] <a [rchild]) {temp = a [rchild]; a [rchild] = a [p]; a [p] = a [rchild];} else break;} return Max ;}} h; int ans; int n, m; int main () {while (scanf ("% d", & n) {h. init (); ans = 0; while (n --) {scanf ("% d", & m); int x; while (m --) {scanf ("% d", & x); h. heap_insert (x);} int Max = h. get_max (); int Min = h. get_min (); ans + = Max-Min;} printf ("% d \ n", ans);} return 0 ;}