Advertisement
warma2d

Untitled

Mar 22nd, 2024
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var readline = require('readline');
  2. var io_interface = readline.createInterface({input: process.stdin});
  3.  
  4. let inputLines = [],
  5.     line_number = 0,
  6.     num_lines;
  7. io_interface.on('line', function (line) {
  8.     if (line_number === 0) {
  9.         num_lines = parseInt(line);
  10.     } else if (line_number <= num_lines) {
  11.         inputLines.push(line);
  12.     }
  13.     line_number++;
  14. })
  15.  
  16. io_interface.on('close', function () {
  17.     const sortedData = quicksortDesc(inputLines);
  18.  
  19.     for (const rowData of sortedData) {        
  20.         process.stdout.write(rowData.split(' ')[0] + "\n");
  21.     }
  22.    
  23. })
  24.  
  25. function partition(array, pivot) {
  26.     let left = [], center = [], right = [];
  27.     for (let x of array) {
  28.         if (x == pivot) {
  29.             center.push(x);
  30.         } else if (isItemLessPivot(x, pivot)) {
  31.             left.push(x);
  32.         } else {
  33.             right.push(x);
  34.         }
  35.     }
  36.     return [right, center, left];
  37. }
  38.  
  39. function isItemLessPivot(item, pivot) {
  40.     let [itemName, numberItemTasks, numberItemErrors] = item.split(' ');
  41.     let [pivotName, numberPivotTasks, numberPivotErrors] = pivot.split(' ');
  42.  
  43.     if (Number(numberItemTasks) < Number(numberPivotTasks)) {
  44.         return true;
  45.     }
  46.  
  47.     if (
  48.         numberItemTasks == numberPivotTasks
  49.         && (Number(numberItemErrors) > Number(numberPivotErrors))
  50.     ) {
  51.         return true;
  52.     }
  53.  
  54.     if (
  55.         (
  56.             numberItemTasks == numberPivotTasks
  57.             && numberItemErrors == numberPivotErrors
  58.         )
  59.         && itemName > pivotName
  60.     ) {
  61.         return true;
  62.     }
  63.  
  64.     return false;
  65. }
  66.  
  67. function quicksortDesc(array) {
  68.     if (array.length < 2) {
  69.         return array;
  70.     } else {
  71.         let pivot = array[Math.floor(array.length/2)];
  72.         let [left, center, right] = partition(array, pivot);
  73.         return quicksortDesc(left).concat(center).concat(quicksortDesc(right));
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement