Gym 102218G. Generating Problems (LICS)

Description
Abraham and Filiberto have been arguing a lot during the last two months. They haven’t decided which problems are going to be used in the 11th edition of the Annual Programming Contest.

As the date of the competition is coming soon, they both prepared different problem sets and now they have several problems that can be used in this contest. Both of them labeled their problems by difficulty d and have arrange them into 2 different arrays, proposing the order that the problems should have during the competition.

Abraham and Filiberto are both very foolish, they proposed the order of the problems and don’t want to change it in any way. That means that if a problem A appears before a problem B in the original array proposal, then in the final problem set, A has to appear before problem B.

After some time of discussing how to choose the problems for the competition, they agreed with the following deal:

They will choose the same quantity of problems from both proposals.
If a problem with difficulty D is chosen from Abraham’s array, then a problem with the same difficulty must be chosen from Filiberto’s array.
Problems must be ordered by strictly increasing difficulty in the final problem set.
And the order of problems in the final problem set, has to preserve the original order in the arrays proposal.
They want to maximize the number of problems that can be used in this competition.

As they are very tired to solve this problem, they ask for your help to compute the answer.

Input
The first line contains two integer numbers n and m (1≤n,m≤104) - size of Filiberto and Abraham’s arrays proposal.

Second line contains n integers di (1≤di≤109) - representing the difficulties of Filiberto’s array proposal.

Next line contains m integers xi (1≤xi≤109) - representing the difficulties of Abraham’s array proposal.

Output
Print one integer - maximum number of problems that can be used in the competition.

Examples
Input
6 5
1 2 1 2 1 3
2 1 3 2 1
Output
4

Note
In the test case you can select 1st and 6th element from the first array and 2nd and 3rd from the second array.

The final problem set difficulties will look as: [1, 1, 3, 3].

Note that numbers marked as bold belong to Abraham’s array proposal and they appear in the same order as in the original array.

Solution
给两个数列 a,b ,求最长上升公共子序列 (LICS) 长度

Code

const int maxn = 2e4 + 7;
int dp[maxn], a[maxn], b[maxn];
int main() {
	int n,m;scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;++i) scanf("%d",&a[i]);
	for(int i = 1;i <= m;++i) scanf("%d",&b[i]);
	for(int i = 1, mx;i <= n;++i) {
		mx = 0;
		for(int j = 1;j <= m;++j) {
			if(a[i] > b[j]) mx = max(mx,dp[j]);
			if(a[i] == b[j]) dp[j] = mx + 1;
		}
	}
	int res = 0;
	for(int i = 1;i <= m;++i) res = max(res,dp[i]);
	printf("%d\n", res<<1);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值