-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (70 loc) · 1.92 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
function conquer(max, items, i, res, best) {
const n = res.reduce((a, c) => a + items[c], 0);
if (n > best.n && n <= max && i < items.length) {
best.n = n;
best.res = res;
}
if (n >= max || i == items.length) return;
conquer(max, items, i + 1, [...res], best);
conquer(max, items, i + 1, [...res, i], best);
}
function divide(max, items, size) {
const overall = { n: 0, res: [] };
let n, smax;
smax = items.length >= size ? Math.round(max / (items.length / size)) : max;
for (let i = 0; i < items.length; i += size) {
if (i + size > items.length) {
n = i + (items.length % size);
smax = max - smax * (i / size);
} else {
n = i + size;
}
const best = { n: 0, res: [] };
conquer(smax, items.slice(i, n), 0, [], best);
overall.res.push(...best.res.map(idx => (idx += i)));
overall.n += best.n;
}
return overall;
}
function linear(max, items) {
const res = [];
let n = 0;
for (let i = items.length - 1; i >= 0; i -= 1) {
if (n + items[i] > max) continue;
res.push(i);
n += items[i];
}
return { n, res };
}
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
const readline = require("readline"),
fs = require("fs");
const rl = readline.createInterface({
input: fs.createReadStream(process.argv[2])
});
const items = [];
let i = 0;
let max;
const delim = /\s+/;
rl.on("line", line => {
const nums = line.split(delim);
if (i == 0) max = Number(nums[0]);
else items.push(...nums.map(n => Number(n)));
i += 1;
});
rl.on("close", () => {
console.log("Input:");
console.log(max, items.length);
// console.log(items);
const best1 = linear(max, items);
const best2 = divide(max, shuffle(shuffle(shuffle(items))), 10);
console.log("Output:");
console.log(best1.n, best1.res.length);
console.log(best2.n, best2.res.length);
});