Binary heap (1): Basic

Source: Internet
Author: User

It turns out that although I know more about the heap theory, I only sorted several heap jobs. I 'd like to review it recently.

Poj3253

{FJ needs to cut a wooden rod into N wooden bars of a given length. Each time the cost is the length of the wooden rod. The total cost and the merged fruit are basically the same, but the title description is inverse, you need to think about cutting as merging. You can combine the smallest wooden bars and record the most cost. Although queuing is the best, it is used to practice basic heap operations. It supports filtering, returns and deletes the minimum element} var A: array [0 .. 1000000] of longint; heapsize: longint; m, n, min1, min2: longint; I, J, K: longint; ans: int64; Procedure heap_down (K: longint ); vaR I, X: longint; begin X: = A [k]; I: = K * 2; While I <= heapsize Do Begin If (I  If A [I] <X then begin a [k]: = A [I]; K: = I; I: = I * 2; End Else   Break ; End; A [k]: = x; end; Procedure swap (var a, B: longint); var C: longint; begin C: = A; A: = B; b: = C; end; function get_min: longint; var X: longint; begin swap (A [1], a [heapsize]); Dec (heapsize ); heap_down (1); exit (A [heapsize + 1]); end; procedure insert (key: longint); var I: longint; begin Inc (heapsize); I: = heapsize; While A [I Div 2]> key Do Begin a [I]: = A [I Div 2]; I: = I Div 2; end; A [I]: = key; end; begin readln (N ); For I: = 1 to n Do Read (A [I]); heapsize: = N; For I: = heapsize Div 2 downto 1 Do Heap_down (I ); For I: = 1 to n-1 Do Begin min1: = get_min; min2: = get_min; Inc (ANS, min1 + min2); insert (min1 + min2); end; writeln (ANS); end.

Poj2051 (wa'sCode)

{Question: Enter n task numbers and latencies. After each task is executed, the latency increases at the start, output the serial number of the previous m tasks in sequence (in the order of serial numbers when the latency is the same ). Create a small root heap. Each time you output the heap top element and add the value of the heap top element keyword and filter it down, I don't know why WA2011-06-19} type data = record delay, time, num: longint end; var: array [0 .. 100000] of data; I, J, K, M, N: longint; s: String [7]; C: Char ; Procedure heap_down (K: longint); var X: data; I: longint; begin X: = A [k]; I: = K * 2; While I <= N Do Begin If (I <n) and (A [I]. delay> A [I + 1]. delay) or (A [I]. delay = A [I + 1]. delay) and (A [I]. num> A [I + 1]. num) Then Inc (I ); If X. Delay> A [I]. Delay then begin a [k]: = A [I]; K: = I; I: = I * 2; End Else  Break ; End; A [k]: = x; end; Procedure inc_key (K, add: longint); begin Inc (A [K]. delay, add); heap_down (1); end; begin While Not EOF Do Begin N: = 0; fillchar (, Sizeof (A), 0); read (c ); While C = 'R'   Do Begin read (C, C); Inc (n); readln (A [n]. num, A [n]. time); A [n]. delay: = A [n]. time; read (c); end; readln (m ); For I: = N Div 2 downto 1 Do Heap_down (I ); For I: = 1 to m Do Begin writeln (A [1]. Num); inc_key (1, a [1]. Time); end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.