-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·131 lines (107 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const { readFile, writeFile } = require('fs');
const { promisify } = require('util');
const read = promisify(readFile);
const write = promisify(writeFile);
const input = {
a: './a_example.txt',
b: './b_read_on.txt',
c: './c_incunabula.txt',
d: './d_tough_choices.txt',
e: './e_so_many_books.txt',
f: './f_libraries_of_the_world.txt',
};
(async () => {
try {
const file = input.f;
const res = await read(file, 'utf8');
let data = res.split('\n');
data = data.splice(0, data.length-1);
data = data.map(x => x.split(' '));
data = data.map(x => x.map(y => +y));
const B = data[0][0];
const L = data[0][1];
const D = data[0][2];
const SCORES = data[1];
const libraries = [];
for (let i = 2; i < data.length-1; i+=2) {
const library = {
count: data[i][0],
process: data[i][1],
bpd: data[i][2], // books per day
registered: false,
id: null,
id1: (i-2)/2,
indexes: data[i+1],
answer: [],
};
// console.log(library.indexes);
libraries.push(library);
// a_example --> scores 1 2 3 6 5 4
// [ { count: 5, process: 2, bpd: 2, indexes: [ 0, 1, 2, 3, 4 ] }, | 1+2+3+6+5=17
// { count: 4, process: 3, bpd: 1, indexes: [ 0, 2, 3, 5 ] } ] | 1+3+6+4=14
}
// LOGIC
let ID = 0;
let max_process = 0;
let lcount = 0;
for (let l = 0; l < L; l++) {
const library = libraries[l];
if (max_process < library.process && !library.registered) {
max_process = library.process;
library.registered = true;
library.id = ID;
ID++;
}
}
let books = 0;
for (let t = 0; t < D; t++) {
if (t === 0)
t += max_process-1;
for (let l = 0; l < L; l++) {
let library = libraries[l];
if (library.registered && library.count) {
if (library.count - library.bpd >= 0) {
books += library.bpd;
library.count -= library.bpd;
}
else {
books += library.count;
library.count = 0;
}
} else {
library.process--;
if (library.process < 1) {
library.registered = true;
library.id = ID;
}
}
}
}
let bcount = 0;
for (let i = 0; i < books-1; i++) {
for (let l = 0; l < libraries.length; l++) {
let library = libraries[l];
if (bcount < books-1) {
if (library.indexes !== undefined && library.indexes[i] !== undefined) {
library.answer.push(library.indexes[i]);
bcount++;
}
}
}
}
let answer = '';
answer += libraries.length + '\n';
libraries.sort((a, b) => a.id > b.id);
for (let library of libraries) {
answer += library.id1 + ' ' + library.answer.length + '\n';
for (let a of library.answer)
answer += a + ' ';
answer += '\n';
}
// console.log(answer);
await write(file + '.out', answer);
} catch (err) {
console.error(err);
}
})();