Nan da algorithm design and Analysis course OJ Answer (3)

Source: Internet
Author: User

Issue A: Dynamic median problem time limit: 1 Sec Memory limit: 8 MB
Submitted by: 866 resolution: 102
Submit State Algorithm FAQ

Title Description

Enter a set of integers A1, A2, ..., an, each input an integer, and the output to the median at this point.

Median definition: If the size of the string is even 2j, the median is the number of small to large line J, if the size of the string is an odd number of 2j+1, the median is from small to large number of j+1.

Enter a set of integers separated by a space between numbers and numbers. Output

A set of integers, separated by a space between numbers and numbers. There are also spaces after the last number.

The integer of the first I output, which is the median of the first I input.

Sample input
Sample output
Tips

Please do not exceed O (NLOGN) for the complexity of the time.
Because the amount of input and output is larger, it is recommended to use the scanf and printf functions in C for input and output, which can save a lot of time than Cin and cout in C + +.

Here was originally a line of hints, but because the teacher accidentally said in class, so the TA cruel to comment it out;

/* When dealing with this problem, the heap structure may provide you with unexpected help. */

Answer

More embarrassing, recently a busy forget to do, so the answer has not been OJ test, for reference only (but the test case over)

The idea is to maintain a mid-value, the largest heap (storing a value smaller than mid), the minimum heap (storing a value larger than mid), each read into a number, according to the size of the heap on both sides to determine the current value, how to judge the code comments, the code in the STL in the use of the heap is not the majority of the

Code run on VS2017, in the Read output section may need to change, scanf_s change scanf?

intmain () {vector<int> Heap_small;//It's supposed to be the biggest heap, with a smaller element than mid.vector<int> heap_big;//It's supposed to be a minimal heap, with elements larger than mid.    intMID =0; scanf_s ("%d",&mid); printf ("%d", mid); inttemp =0;  while(scanf_s ("%d", &temp)! =EOF) {        if(Temp <mid) {if(heap_small.size () = =heap_big.size ()) {                //The left side is as large as the right side, the mid is placed in the Heap_big, temp is placed in the Heap_small, and a maximum of mid is selected from Heap_smallHeap_big.emplace_back (mid); Push_heap (Heap_big.begin (), Heap_big.end (), [] (intAintb) {returnb <A;});                Heap_small.emplace_back (temp);                Push_heap (Heap_small.begin (), Heap_small.end ());                Pop_heap (Heap_small.begin (), Heap_small.end ()); Mid=Heap_small.back ();            Heap_small.pop_back (); }Else if(heap_small.size () = = Heap_big.size ()-1) {                //The right side is one more than the left, put temp in heap_small, then mid or medianHeap_small.emplace_back (temp);            Push_heap (Heap_small.begin (), Heap_small.end ()); }Else {                //One more left than the right should not appearcout <<"wrong"<<Endl; }        }        Else {            if(heap_small.size () = =heap_big.size ()) {                //left and right as large, put temp in heap_big, mid or medianHeap_big.emplace_back (temp); Push_heap (Heap_big.begin (), Heap_big.end (), [] (intAintb) {returnb <A;}); }            Else if(heap_small.size () = = Heap_big.size ()-1) {                //The right side is one more than the left, put mid in heap_small, then put temp in Heap_big, then choose a minimum from Heap_big as the midHeap_small.emplace_back (mid);                Push_heap (Heap_small.begin (), Heap_small.end ());                Heap_big.emplace_back (temp); Push_heap (Heap_big.begin (), Heap_big.end (), [] (intAintb) {returnb <A;}); Pop_heap (Heap_big.begin (), Heap_big.end (), [] (intAintb) {returnb <A;}); Mid=Heap_big.back ();            Heap_big.pop_back (); }            Else {                //One more left than the right should not appearcout <<"wrong"<<Endl; }} printf ("%d", mid); }return 0;}

Question B: height and diameter of imperfect binary tree time limit: 2 Sec Memory limit: 3 MB
Submitted by: 249 Resolution: 95
Submit State Algorithm FAQ Title Description

Remember T is a binary tree, the tree has n nodes.

Defines the depth of the root node as 0, and the depth of the remaining nodes is the depth of its parent node plus 1. The height of T is defined as the maximum value of its leaf node depth.

Defines the distance between any two points A and B in the tree as the length of the shortest simple path. The diameter of T is defined as the maximum value of a bit of distance between the T.

Enter a binary tree T, please calculate its height and diameter.

Input

Enter a total of three rows.

The first line enters the value of N, which represents the total number of nodes in the tree.

The first sequence traversal of the second behavior tree indicates that each node is separated by a space.

The middle sequence traversal of the third behavior tree means that each node is also separated by a space.

Output

Output total three rows.
The first line requires you to output a line of string, which is "I have read the description of plagiarism" in English translation, namely: "I have read the rules about plagiarism punishment". Output the submission of this line we will assume that we have fully read and understood the "description of plagiarism" announcement and agree to the punitive measures on plagiarism.
The height of the second row of the output tree.
The diameter of the third row of the output tree.



Sample input
Sample output
I have read the rules about plagiarism Punishment35
Tips

The divide-and-conquer algorithm can complete the calculation in O (n) time.

Answer

Can't submit, too lazy to write, online answers quite a lot of

Nan da algorithm design and Analysis course OJ Answer (3)

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.