AcWing 1014. 登山 (JavaScript )
原题链接
简单
作者:
cp777
,
2021-03-09 10:58:03
,
所有人可见
,
阅读 386
const N = 1010;
let g = [];
let l = new Int32Array(N).fill(1); //从左往右的
let r = new Int32Array(N).fill(1); //自身上升子序列长度为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);
g[0] = 0;
for (let i = 1; i <= arr.length; i++) g[i] = arr[i - 1];
for (let i = 1; i <= n; i++) {
for (let j = 1; j < i; j++) {
if (g[i] > g[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 (g[i] > g[j]) {
r[i] = Math.max(r[i], r[j] + 1);
}
}
}
let res = 0;
for (let i = 1; i <= n; i++) res = Math.max(res, r[i] + l[i] - 1);
console.log(res);
}
});
});