题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748
一开始小编以为f就是表示的是到这一位的最长上升子序列长度,于是直接不是当前最大值就填1,WA了两发才发现是以这一位结束的最长上升子序列长度,那么问题其实求出每一位的f,f就是答案。
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int a[maxn],dp[maxn],n,L[maxn];
void get_len()
{
int len = 1,pos,num = 1;
dp[1] = a[1];
L[1] = 1;
for(int i=2; i<=n; i++)
{
if(a[i] > dp[len])
{
//printf("%d %d\n", a[i],dp[len]);
dp[++len] = a[i];
L[i] = len;
}
else
{
pos = lower_bound(dp+1, dp+len+1, a[i])-dp;
dp[pos] = a[i];
//printf("%d\n", pos);
L[i] = pos;
}
}
}
int main()
{
int T,t=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
//a[i] -= i;
}
get_len();
printf("%d", L[1]);
for(int i=2; i<=n; i++)
printf(" %d", L[i]);
printf("\n");
}
}