Advertisement
Queses

ITMO Algorithms Course: Week 3 Task 1 written on JavaScript

Mar 17th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const EdxIo = require('./EdxIo')
  2.  
  3. const NUMBER_BASE = 512
  4. const NUMBER_BASE_MAX = 511
  5. const NUMBER_BASE_POW = 9
  6. const MAX_NUM_LENGTH = 4
  7.  
  8. class Week3Task1 {
  9.     run () {
  10.         this.io = new EdxIo()
  11.         this.buildArray()
  12.         this.digitSort()
  13.         this.writeOutput()
  14.  
  15.         return this.io.close()
  16.     }
  17.    
  18.     countSort (bufferArr, counters, digit) {
  19.         counters.fill(0)
  20.    
  21.         const itemShift = NUMBER_BASE_POW * digit
  22.         for (let i = 0; i < this.arr.length; i++) {
  23.             counters[(this.arr[i] >> itemShift) & NUMBER_BASE_MAX]++
  24.         }
  25.    
  26.         let prevCounters = counters[0]
  27.         for (let i = 1; i < NUMBER_BASE; i++) {
  28.             if (counters[i] > 0) {
  29.                 counters[i] += prevCounters
  30.                 prevCounters = counters[i]
  31.             }
  32.         }
  33.    
  34.         for (let i = this.arr.length - 1; i >= 0; i--) {
  35.             const value = this.arr[i]
  36.             bufferArr[--counters[(value >> itemShift) & NUMBER_BASE_MAX]] = value
  37.         }
  38.     }
  39.  
  40.     digitSort () {
  41.         let bufferArr = new Uint32Array(this.arr.length)
  42.         const counters = new Uint16Array(NUMBER_BASE)
  43.         for (let digit = 0; digit < MAX_NUM_LENGTH; digit++) {
  44.             this.countSort(bufferArr, counters, digit)
  45.             const arrTmp = this.arr
  46.             this.arr = bufferArr
  47.             bufferArr = arrTmp
  48.         }
  49.     }
  50.  
  51.     writeOutput () {
  52.         let tenthSum = 0
  53.         for (let i = 0; i < this.arr.length; i += 10) {
  54.             tenthSum += this.arr[i]
  55.         }
  56.  
  57.         this.io.write(tenthSum)
  58.     }
  59.    
  60.     buildArray () {
  61.         const firstLength = this.io.nextInt()
  62.         const secondLength = this.io.nextInt()
  63.  
  64.         const firstArr = new Uint32Array(firstLength)
  65.         for (let i = 0; i < firstLength; i++) {
  66.             firstArr[i] = this.io.nextInt()
  67.         }
  68.  
  69.         this.arr = new Uint32Array(firstLength * secondLength)
  70.         let offset = 0
  71.         let value
  72.         for (let j = 0; j < secondLength; j++) {
  73.             value = this.io.nextInt()
  74.             for (let i = 0; i < firstLength; i++) {
  75.                 this.arr[i + offset] = firstArr[i] * value
  76.             }
  77.                        
  78.             offset += firstLength
  79.         }
  80.     }
  81. }
  82.  
  83. new Week3Task1().run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement