AcWing 896. 最长上升子序列 II. (Javascript)
原题链接
中等
作者:
cp777
,
2021-03-05 17:08:50
,
所有人可见
,
阅读 294
const N = 100010;
let f = new Int32Array(N); //f[i] : 长度为i的子序列结尾下标最小为
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) {
a = getInputNums(line);
let len = 0; //初始f长度为0;
for (let i = 0; i < n; i++) {
let l = 0,
r = len;
while (l < r) {
let mid = parseInt((l + r + 1) / 2);
if (f[mid] < a[i]) l = mid;
else r = mid - 1;
}
// 二分找的是可以接在哪个数的后面
f[r + 1] = a[i];
if (r + 1 > len) len++;
}
console.log(len);
}
});
});