Advertisement
Guest User

Untitled

a guest
May 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const binarySearch = function (start, finish, value)
  2. {
  3.     let guess;
  4.     let min = start;
  5.     let max = finish;
  6.  
  7.     while (min <= max)
  8.     {
  9.         guess = Math.floor((min + max) / 2);
  10.         if (guess === value)
  11.             return guess;
  12.         else if (guess < value)
  13.             min = guess + 1;
  14.         else
  15.             max = guess - 1;
  16.     }
  17.  
  18.     return -1;
  19. };
  20.  
  21. let lineReader = require('readline').createInterface({
  22.                                                          input: require('fs').createReadStream('input.txt')
  23.                                                      });
  24.  
  25. lineReader.on('line', function (line)
  26. {
  27.     if (line !== '')
  28.     {
  29.  
  30.         let haveAns = false;
  31.  
  32.         let row_input = line.split(' ');
  33.  
  34.         const s = parseInt(row_input[0]);
  35.         const l1 = parseInt(row_input[1]);
  36.         const r1 = parseInt(row_input[2]);
  37.         const l2 = parseInt(row_input[3]);
  38.         const r2 = parseInt(row_input[4]);
  39.  
  40.         for (let i = l1; i <= r1; i++)
  41.         {
  42.             let tmp = binarySearch(l2, r2, s - i);
  43.             if (tmp !== -1)
  44.             {
  45.                 console.log(i, tmp);
  46.                 haveAns = true;
  47.                 break;
  48.             }
  49.         }
  50.  
  51.         if (!haveAns)
  52.         {
  53.             console.log(-1);
  54.         }
  55.     }
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement