Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const EdxIo = require('./EdxIo')
- const NUMBER_BASE = 512
- const NUMBER_BASE_MAX = 511
- const NUMBER_BASE_POW = 9
- const MAX_NUM_LENGTH = 4
- class Week3Task1 {
- run () {
- this.io = new EdxIo()
- this.buildArray()
- this.digitSort()
- this.writeOutput()
- return this.io.close()
- }
- countSort (bufferArr, counters, digit) {
- counters.fill(0)
- const itemShift = NUMBER_BASE_POW * digit
- for (let i = 0; i < this.arr.length; i++) {
- counters[(this.arr[i] >> itemShift) & NUMBER_BASE_MAX]++
- }
- let prevCounters = counters[0]
- for (let i = 1; i < NUMBER_BASE; i++) {
- if (counters[i] > 0) {
- counters[i] += prevCounters
- prevCounters = counters[i]
- }
- }
- for (let i = this.arr.length - 1; i >= 0; i--) {
- const value = this.arr[i]
- bufferArr[--counters[(value >> itemShift) & NUMBER_BASE_MAX]] = value
- }
- }
- digitSort () {
- let bufferArr = new Uint32Array(this.arr.length)
- const counters = new Uint16Array(NUMBER_BASE)
- for (let digit = 0; digit < MAX_NUM_LENGTH; digit++) {
- this.countSort(bufferArr, counters, digit)
- const arrTmp = this.arr
- this.arr = bufferArr
- bufferArr = arrTmp
- }
- }
- writeOutput () {
- let tenthSum = 0
- for (let i = 0; i < this.arr.length; i += 10) {
- tenthSum += this.arr[i]
- }
- this.io.write(tenthSum)
- }
- buildArray () {
- const firstLength = this.io.nextInt()
- const secondLength = this.io.nextInt()
- const firstArr = new Uint32Array(firstLength)
- for (let i = 0; i < firstLength; i++) {
- firstArr[i] = this.io.nextInt()
- }
- this.arr = new Uint32Array(firstLength * secondLength)
- let offset = 0
- let value
- for (let j = 0; j < secondLength; j++) {
- value = this.io.nextInt()
- for (let i = 0; i < firstLength; i++) {
- this.arr[i + offset] = firstArr[i] * value
- }
- offset += firstLength
- }
- }
- }
- new Week3Task1().run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement