HDOJ 5748 Bellovin

题目链接: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");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值