题目链接
hdu 5748 http://acm.hdu.edu.cn/showproblem.php?pid=5748
poj 2533 http://poj.org/problem?id=2533
题解:
每次将数字按照大小顺序插入到一个数组里,将插入位置的数字替换掉,插入的位置即是当前位置最长上升子序列的长度。
AC代码:
//hdu 5748
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define INF 1e9
using namespace std;
const int maxn = 100010;
int dp[maxn];
int a[maxn];
int b[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
for(int i = 1; i <= n; i++)b[i] = INF;
for(int i = 1; i <= n; i++)
{
int pos = lower_bound(b+1,b+n+1,a[i])-b;
b[pos] = a[i];
dp[i] = pos;
}
int maxn = 0;
for(int i = 1; i < n; i++)maxn = max(maxn,dp[i]);
printf("%d\n",maxn);
}
return 0;
}
//poj 2533
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define INF 1e9
using namespace std;
const int maxn = 100010;
int dp[maxn];
int a[maxn];
int b[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
for(int i = 1; i <= n; i++)b[i] = INF;
for(int i = 1; i <= n; i++)
{
int pos = lower_bound(b+1,b+n+1,a[i])-b;
b[pos] = a[i];
dp[i] = pos;
}
int maxn = 0;
for(int i = 1; i <= n; i++)maxn = max(maxn,dp[i]);
printf("%d\n",maxn);
}
return 0;
}