-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathB.js
More file actions
49 lines (39 loc) · 1.69 KB
/
B.js
File metadata and controls
49 lines (39 loc) · 1.69 KB
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
/**
* "B. Таможня" {@link "https://contest.yandex.ru/contest/29396/problems/B/}
*
* ...какое минимальное количество аппаратов необходимо заказать на заводе, если на одном аппарате
* может одновременно обрабатываться только один груз
*
* @param { number } n длина массива (0 ≤ N ≤ 50 000)
* @param { Array<number> } segments - Ti и Li – время прибытия соответствующего груза и время,
* требуемое для его обработки (1 ≤ Ti ≤ 10^6, 1 ≤ Li ≤ 10^6).
*
* @return { number } наименьшее количество аппаратов
*/
function maxCountSegmentsAtSameTime(n, segments) {
const EVENT_TYPE = { begin: 0, end: -1 }; // сначала освобождение аппарата
const events = [];
for (let i = 0; i < n; i++) {
const [begin, duration] = segments[i];
events.push({ time: begin, type: EVENT_TYPE.begin });
events.push({ time: begin + duration, type: EVENT_TYPE.end });
}
events.sort((prev, next) => prev.time - next.time || prev.type - next.type);
let nowCount = 0;
let max = 0;
for (let i = 0; i < n * 2; i++) {
if (events[i].type === EVENT_TYPE.begin) {
nowCount += 1;
} else if (events[i].type === EVENT_TYPE.end) {
nowCount -= 1;
}
max = Math.max(max, nowCount);
}
return max;
}
function inputProcessing(lines) {
const n = Number(lines[0]);
const segmentsLR = lines.slice(1).map((lr) => lr.split(' ').map(Number));
return maxCountSegmentsAtSameTime(n, segmentsLR);
}
export default inputProcessing;