AcWing 895. 最长上升子序列. (JavaScript )
原题链接
简单
作者:
cp777
,
2021-03-05 11:39:56
,
所有人可见
,
阅读 282
const N = 1010;
let arr = [];
let f = new Int32Array(N).fill(1); //最长序列只有自己一个数
let a = new Int32Array(N);
let n = 0;
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) {
arr = getInputStr(line);
for (let i = 0; i < arr.length; i++) a[i + 1] = arr[i];
for (let i = 1; i <= n; i++) {
for (let j = 1; j < i; j++)
if (a[j] < a[i])
f[i] = Math.max(f[i], f[j] + 1);
}
let res = 0;
for (let i = 1; i <= n; i++) res = Math.max(res, f[i]);
console.log(res);
}
});
});