AcWing 482. 合唱队形 (JavaScript )
原题链接
中等
作者:
cp777
,
2021-03-10 10:56:16
,
所有人可见
,
阅读 464
const N = 110;
let q = [];
let l = new Int32Array(N).fill(1);
let r = new Int32Array(N).fill(1);
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputNums = line => line.split(' ').filter(s => s !== '').map(x => parseInt(x));
let getInputStr = line => line.split(' ').filter(s => s !== '');
process.stdin.on('end', function () {
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
n = getInputNums(line)[0];
} else if (lineIdx === 1) {
let arr = getInputNums(line);
for (let i = 1; i <= arr.length; i++) q[i] = arr[i - 1];
for (let i = 1; i <= n; i++) {
for (let j = 1; j < i; j++) {
if (q[i] > q[j]) l[i] = Math.max(l[i], l[j] + 1);
}
}
for (let i = n; i >= 1; i--) {
for (let j = n; j > i; j--) {
if (q[i] > q[j]) r[i] = Math.max(r[i], r[j] + 1);
}
}
let max = 0;
for (let i = 1; i <= n; i++) max = Math.max(max, l[i] + r[i] - 1);
console.log(n - max);
}
});
});