-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb17299.js
More file actions
37 lines (30 loc) · 679 Bytes
/
b17299.js
File metadata and controls
37 lines (30 loc) · 679 Bytes
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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const seq = input[1].split(" ").map(Number);
const F = Array(1000001).fill(0);
const opposite = [];
for (const n of seq) {
F[n] += 1;
}
let result = "-1";
opposite.push(seq.pop());
while (seq.length) {
const top = seq.pop();
let isNGF = false;
let i = opposite.length - 1;
while (opposite.length) {
const n = opposite[i--];
if (F[n] > F[top]) {
result = `${n} ${result}`;
isNGF = true;
break;
} else {
opposite.pop();
}
}
if (!isNGF) {
result = `-1 ${result}`;
}
opposite.push(top);
}
console.log(result.trim());