#include <iostream>
#include <fstream>
#include <assert.h>
#include <string>
using namespace std;

   ifstream in;

class Node
   {
      private:
         Node *smaller, *bigger;
      int recordNumber;
      int year;
      public:
         Node(int number, int yr)
         {
         smaller = 0;
         bigger = 0;
         recordNumber = number;
         year = yr;
         }

      void addRecord(int number, int yr)
         {
         if(yr <= year)
            {
            if(smaller == 0)
               {
               smaller = new Node(number, yr);
               }
            else
               {
               smaller->addRecord(number, yr);
               }
            }
         else if(yr > year)
            {
            if(bigger == 0)
               {
               bigger = new Node(number, yr);
               }
            else
               {
               bigger->addRecord(number, yr);
               }
            }
         }

      void print()
         {
         if(bigger != 0)
            bigger->print();
         string line;
         in.seekg(recordNumber*53+1, ios::beg);
         assert(!in.fail());
         //cout << "Printing" << endl;
         getline(in, line, '\n');
         assert(!in.fail());
         cout << line << endl;
         if(smaller != 0)
            smaller->print();
         }
   };

class Head
   {
      private:
      Node *tree;
      public:
         Head()
         {
         tree= 0;
         }

      void addRecord(int number, int yr)
         {
         if(tree == 0)
            tree = new Node(number, yr);
         else
            {
            tree->addRecord(number, yr);
            }
         }

      void print()
         {
         if(tree == 0)
            cout << "No data." << endl;
         else
            tree->print();
         }

   };

int getCost(int record)
   {
   in.seekg(record*53+1, ios::beg);
   assert(!in.fail());
   int cost;
   in >> cost;
   in.seekg(0, ios::beg);
   return cost;
   }

int getYear(int record)
   {
   in.seekg(record*53+14, ios::beg);
   assert(!in.fail());
   int year;
   in >> year;
   in.seekg(0, ios::beg);
   return year;
   }

int findTheRecord(int cost, int upper, int lower)
   {

   int uppercost = getCost(upper);
   int lowercost = getCost(lower);
   int middlerecord = upper + (int)((lower-upper)/2);
   int middlecost = getCost(middlerecord);

   if(middlerecord == lower || middlerecord == upper)
      return -1;
   if(cost == uppercost)
      return upper;
   if(cost == lowercost)
      return lower;
   if(cost == middlecost)
      return middlerecord;

   if(middlecost < cost)
      return findTheRecord(cost, upper, middlerecord);
   if(middlecost > cost)
      return findTheRecord(cost, middlerecord, lower);
   return -1;
   }

int findFirstRecord(int cost, int recordNumber)
   {
   if(recordNumber == 0 && getCost(recordNumber) == cost)
      return 0;
   else if(recordNumber == 0)
      {
      return 1;
      }
   else
      {
      if(getCost(recordNumber) == cost)
         {
         return findFirstRecord(cost, recordNumber -1);
         }
      else
         return recordNumber + 1;
      }
   }

int main()
   {
     in.open ("planelist.data", ios::binary );
   assert(!in.fail());

   // get length of file:
   int length;
     in.seekg (0, ios::end);
     length = in.tellg();
     in.seekg (0, ios::beg);

   int upperrecord = 0;
   int lowerrecord = length/53-1;
   int endRecord = lowerrecord;
   int middlerecord = (int)((lowerrecord-upperrecord)/2);
   int search;
   cout << "Enter Cost:  ";
   cin >> search;

   int uppercost = getCost(upperrecord);
   int lowercost = getCost(lowerrecord);
   int middlecost = getCost(middlerecord);

   int aRecordNumber = findTheRecord(search, upperrecord, lowerrecord);
   if(aRecordNumber == -1)
      {
      cout << "No Records Found." << endl;
      in.close();
      exit(1);
      }
   int firstRecord = findFirstRecord(search, aRecordNumber);
   Head *h;
   h = new Head();
   while(getCost(firstRecord) == search)
      {
      h->addRecord(firstRecord, getYear(firstRecord));
      if(firstRecord != endRecord)
         firstRecord++;
      else
         break;
      //cout << "Added a record and increased to next" << endl;
      }
   h->print();
   in.close();
   }