短学期 dp

求不上升序列长度和一个上升序列长度

#include<iostream>
#include<algorithm>
using namespace std;
int p[100010], t[100010], k[100010];
int n;
int main() {
	cin >> n;
	int i, len = 1, my_len = 1;
	ios_base::sync_with_stdio(false);
	for (i = 1; i <= n; i++) {
		cin >> p[i];
	}
	k[1] = t[1] = p[1];
	for (i = 2; i <= n; i++) {
		if (p[i] <= t[len]) {
			t[++len] = p[i];
		}
		else {
			*upper_bound(t + 1, t + len + 1, p[i], greater<int>()) = p[i];
		}
		if (p[i] > k[my_len]) {
			k[++my_len] = p[i];
		}
		else {
			*lower_bound(k + 1, k + my_len + 1, p[i]) = p[i];
		}
	}
	//len是不上升序列长度,my_len是上升序列长度
	cout << len << endl;
	cout << my_len << endl;
	return 0;
}

https://www.luogu.com.cn/problem/P1439(最长公共子序列)
思路:
5
a:3 2 1 4 5
b:1 2 3 4 5
以a的顺序来寻找b的最长上升子序列(即可以看成重新定义大小关系)3<2<1<4<5

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100001], b[100001], map[100001], f[100001];
int main()
{
	int n;
	ios_base::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) { 
		cin >> a[i];
		map[a[i]] = i;
	
	}
	for (int i = 1; i <= n; i++) { 
		cin >> b[i];
	
	}
	int len = 1;
	f[1] = map[b[1]];
	for (int i = 1; i <= n; i++)
	{
		
		if (map[b[i]] > f[len])
			f[++len] = map[b[i]];
		else
		{
			*lower_bound(f + 1, f + len + 1, map[b[i]]) = map[b[i]];
		}
	}
	cout << len;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值