A "Median maintenance" problem

Source: Internet
Author: User

Title Requirements:

Download the text file here. (Right click and save link as).

The goal of this problem are to implement a variant of the 2-sum algorithm (covered in the Week 6 lecture on hash table app lications).

The file contains 1 million integers, both positive and negative (there might be some repetitions!). This was your array of integers, with thei< Span id= "mathjax-span-5" class= "Texatom" > th row of the file specifying the  i t H entry of the array.
/span>

Your task is to compute the number of target valuesT in the interval [ -10000,10000] (inclusive) such that there isdistinctNumbers< Span id= "mathjax-span-21" class= "Mrow" >x ,y in the input file that satisfy  x< Span id= "mathjax-span-28" class= "Mo" >+y =t. (note:ensuring distinctness Requires a one-line addition to the algorithm from lecture.)

Write Your numeric answer (an integer between 0 and 20001) in the space provided.

OPTIONAL Challenge:if This problem are too easy for your, try implementing your own hash table for it. For example, you could compare performance under the chaining and open addressing approaches to resolving collisions.

The general meaning is that there are 1 million numbers (both positive and negative, and may be repeated). The goal is to find two numbers x and Y, so that x+y belongs to the range of [-10000, 10000], and X and Y are the only ones in all numbers, and the final output is the number of all possible ones.

The data in the file is almost like this:

...

6195
2303
5685
1354
4292
7600
6447
4479
9046
7293
5147
1260
1386
6193
4135
3611
8583

...

Problem Solving Ideas:

This problem can of course use the most violent method, that is, each time you read a number to sort the array, and then record the median, but obviously there should be a better way. Yes, if the data structure of "heap" is borrowed, the time complexity of the algorithm can be greatly reduced. The specific ideas are as follows:

    • Create two heaps: maximum heap and minimum heap (the largest heap is the heap where the parent node is greater than the child node, and the other is the smallest heap);
    • Each time a number is read, we compare it to the maximum heap with the minimum heap root node size, if it is greater than the maximum heap root node, then it is inserted into the minimum heap, instead, it is inserted in the largest heap. As you can imagine, the number larger than the two root nodes is below the root node of the smallest heap, and the number smaller than the two root nodes is below the root node of the maximum heap;
    • With this conclusion, we cannot guarantee that the median is in the two root nodes, because the size of the two heap may be very poor, so after each reading into a number and inserting the appropriate heap, we have to check the size of the two heap, and then balance their size (only if the size difference of two heap is not greater than 1) The median is one of the two root nodes)
    • The exact balance is: if the size difference of the two heap exceeds 1, then pop out the root node of the larger heap and insert it into the smaller size heap;
    • Finally, the median is calculated because the root node of the minimum heap is greater than the root node of the largest heap, so if the minimum heap size is 1 larger than the maximum heap, the median is the minimum heap root node, and if both sizes are equal, or if the largest heap is 1 larger than the smallest heap, the median is the root node of the largest heap.

Code implementation:

With the above ideas, using C + + to implement it, the code is as follows:

#include <iostream>#include<fstream>#include<string>#include<sstream>#include<limits>using namespacestd;classMinmaxheap { Public: Minmaxheap (BOOLis_min); ~minmaxheap (); intTop (); intSize (); voidInsert (intnum); voidPop ();Private:    voidSwapintIndex1,intindex2); intsize; int*element; BOOLis_min;}; Minmaxheap::minmaxheap (BOOLIs_min =true) {    //for the problem, 5010 is just fine     This->element =New int[5010];  This->size =0;  This->is_min =Is_min;} Minmaxheap::~minmaxheap () {Delete[] This-element;}intMinmaxheap::top () {return  This->element[0];}intminmaxheap::size () {return  This-size;}//The position of each element (the number means the array of the index)//0//         /   //1 2//      /  \    /  //3 4 5 6voidMinmaxheap::insert (intnum) {    intpos =bsize; Element[size++] =num; if(is_min) { while(Pos >0) {            intParent = (POS-1) >>1;//Same as (pos-1)/2            if(Element[parent] <=Element[pos]) {                 Break;            } Swap (parent, POS); POS=parent; }    }    Else {         while(Pos >0) {            intParent = (POS-1) >>1;//Same as (pos-1)/2            if(Element[parent] >=Element[pos]) {                 Break;            } Swap (parent, POS); POS=parent; }    }}voidminmaxheap::P op () {element[0] = element[--size]; intpos =0; if(is_min) { while(Pos < (size >>1))//if Pos >= (SIZE/2), then Element[pos] must is a leaf        {            intLeft_child = pos *2+1; intRight_child = Left_child +1; intSmallest_child; if(Right_child < size && Element[left_child] >Element[right_child]) {Smallest_child=Right_child; }            Else{smallest_child=Left_child; }            if(Element[pos] <Element[smallest_child]) {                 Break;            } swap (POS, smallest_child); POS=Smallest_child; }    }    Else {         while(Pos < (size >>1))//if Pos >= (SIZE/2), then Element[pos] must is a leaf        {            intLeft_child = pos *2+1; intRight_child = Left_child +1; intBiggest_child; if(Right_child < size && Element[left_child] <Element[right_child]) {Biggest_child=Right_child; }            Else{biggest_child=Left_child; }            if(Element[pos] >Element[biggest_child]) {                 Break;            } swap (POS, biggest_child); POS=Biggest_child; }    }}voidMinmaxheap::swap (intIndex1,intindex2) {    intTMP =ELEMENT[INDEX1]; ELEMENT[INDEX1]=Element[index2]; ELEMENT[INDEX2]=tmp;}intMain () {ifstream fin; Fin.open ("Median.txt"); Minmaxheap Minheap (true); Minmaxheap Maxheap (false); //because we want to find the median, so insert//both min of int and max of int is ok.Minheap.insert (numeric_limits<int>:: Max ()); Maxheap.insert (Numeric_limits<int>:: Min ()); intInput, sum =0, Min_top, max_top; stringtmp;  while(Getline (FIN, tmp)) {input=atoi (Tmp.c_str ()); Min_top=Minheap.top (); Max_top=Maxheap.top (); if(Input <max_top)        {Maxheap.insert (input); }        Else{Minheap.insert (input); }        //Balance        if(Maxheap.size () > minheap.size () +1) {Max_top=Maxheap.top ();            Maxheap.pop ();        Minheap.insert (Max_top); }        if(Minheap.size () > maxheap.size () +1) {Min_top=Minheap.top ();            Minheap.pop ();        Maxheap.insert (Min_top); }        //Find the median        if(minheap.size () = = Maxheap.size () +1) {sum+=Minheap.top (); }        Else{sum+=Maxheap.top (); }} cout<< Sum%10000<<Endl;    Fin.close (); System ("Pause"); return 0;}

By this method, the efficiency of operation is greatly improved.

A "Median maintenance" problem

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.