二分
求一个序列的最长上升子序列个数。
本程序采用边读边处理 + 二分法。
ll f[maxn], ans = 1; //注意答案个数初始化为1
int main()
{
ll n = read();
for (int i = 1; i <= n; ++i)
{
int x = read();
if (i == 1)
{
f[1] = x;
continue;
}
int l = 1, r = ans, mid;
while (l <= r)
{
mid = (l + r) >> 1;
if (x <= f[mid])
r = mid - 1;
else
l = mid + 1;
}
f[l] = x;
if (l > ans)
++ans;
}
printf("%lld\n", ans);
return 0;
}
dp
int f[1050][1050], n, l, k;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> f[i][1];
f[i][2] = 1;
f[i][3] = 0;
}
for (int i = n - 1; i >= 1; --i)
{
l = 0, k = 0;
for (int j = i + 1; j <= n; ++j)
if ((f[j][1] > f[i][1]) && (f[j][2] > l))
{
l = f[j][2];
k = j;
}
if (l > 0)
{
f[i][2] = l + 1;
f[i][3] = k;
}
}
k = 1;
for (int j = 1; j <= n; ++j)
if (f[j][2] > f[k][2])
k = j;
cout << f[k][2] << endl;
return 0;
}