【模板】最长公共子序列(二维偏序)

给出1-n的两个排列P1和P2,求它们的最长公共子序列。

洛谷1439

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxn=100010;
 5 int n,x,ans,tmp,pos[maxn],t[maxn];
 6 void read(int &k){
 7     k=0; int f=1; char c=getchar();
 8     while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar();
 9     while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar();
10     k*=f;
11 }
12 void add(int x,int y){for(;x<=n;x+=(x&-x)) t[x]=max(t[x],y);}
13 int query(int x){int ret=0;for(;x>0;x-=(x&-x))ret=max(ret,t[x]);return ret;}
14 int main(){
15     read(n);
16     for (int i=1;i<=n;i++) read(x),pos[x]=i;
17     for (int i=1;i<=n;i++) read(x),tmp=query(pos[x]),ans=max(ans,tmp+1),add(pos[x],tmp+1);
18     return printf("%d\n",ans),0;
19 }

时间: 2024-07-31 20:41:04

【模板】最长公共子序列(二维偏序)的相关文章

14-高效求最长公共子序列(二维数组存不下)

/*                                   See LCS again时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述 There are A, B two sequences, the number of elements in the sequence is n.m; Each element in the sequence are different and less than 100000. Calculate the length

模板 最长公共子序列

[模板]最长公共子序列 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char s1[1000],s2[1000]; 7 int len1,len2,dp[1000][1000],mark[1000][1000];//如果数据太大,dp数组可以考虑滚动数组 8 9 void LCS() 10 { 11 int i,j; 12 mem

模板 最长公共递增子序列

[模板]最长递增公共子序列 二维 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 6 int n,m,a[505],b[505],dp[505][505]; 7 8 int LICS() 9 { 10 int MAX,i,j; 11 memset(dp,0,sizeof(dp)); 12 for(i = 1; i<=n; i++)

51nod 1376: 最长递增子序列的数量(二维偏序+cdq分治)

1376 最长递增子序列的数量 Time Limit: 1 Sec Memory Limit: 128MB 分值: 160 难度:6级算法题 Description 数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可能有很多个.例如A为:{1 3 2 0 4},1 3 4,1 2 4均为A的LIS.给出数组A,求A的LIS有多少个.由于数量很大,输出Mod 1

洛谷 P1439 【模板】最长公共子序列

神TM模板..我本来想休闲一下写点水题的... 开始做的时候直接敲了一个O(N2)的算法上去,编译的时候才发现根本开不下.. 好了,谈回这道题. 先不加证明的给出一种算法. 若有一组数据 2 4 2 5 1 3 2 5 4 1 3 那么我们令 4 2 5 1 3 | | | | | 1 2 3 4 5 第三行的数据就变成 2 3 1 4 5 很明显,答案是这个数据的最长上升子序列,即4 == 2 3 4 5,即原数列的2 5 1 3. 现在来大概的介绍一下这样做的原因. 首先,观察题目,注意到这

【luogu1439】 【模板】最长公共子序列 [动态规划][LIS最长上升子序列][离散化]

P1439 [模板]最长公共子序列 此思路详见luogu第一个题解 一个很妙的离散化 刘汝佳蓝书上面的LIS 详见蓝书 d[i]以i为结尾的最长上升子序列的长度     g[i]表示d值为i的最小状态的编号即长度为i的上升子序列的最小末尾值 1 for(int i=1;i<=n;++i) scanf("%d",&a[i]); 2 for(int i=1;i<=n;++i) 3 { 4 int k=lower_bound(g+1,g+1+n,a[i])-g; 5 d[

P1439 【模板】最长公共子序列 题解

CSDN同步 原题链接 简要题意: 给定两个 \(1\) ~ \(n\) 的排列,求其 最长公共子序列. 嗯,下面给出若干算法吧. 算法一 不管它是 \(1\) ~ \(n\) 的排列这一性质. 求 \(\text{LCS}\)(即最长公共子序列)的套路方法: 用 \(f_{i,j}\) 表示 \(a_1\) ~ \(a_i\) 和 \(b_1\) ~ \(b_j\) 的最长公共子序列.那么不考虑边界问题,则存在: \[f_{i,j} = \begin{cases} f_{i-1,j-1}+ 1

动态规划二:最长公共子序列(LCS)

1.两个子序列:X={x1,x2....xm},Y={y1,y2....yn},设Z={z1,z2...zk}. 2.最优子结构: 1)如果xm=yn ,则zk=xm=yn且Zk-1是Xm-1和Yn-1的一个LCS. 2)如果xm!=yn ,则zk!=xm包含Z是Xm-1和Y的一个LCS. 3)如果xm!=yn ,则zk!=yn包含Z是X和Yn-1的一个LCS. 3.则由最优子结构可得递归式: 4.实现: 1 #include<string> 2 #include<iostream>

Luogu 1439 【模板】最长公共子序列

题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子序列的长度 输入输出样例 输入样例#1: 5 3 2 1 4 5 1 2 3 4 5 输出样例#1: 3 说明 [数据规模] 对于50%的数据,n≤1000 对于100%的数据,n≤100000 [题解] 首先不难想到的是将其转化成一个序列的最长上升子序列 由于两个序列均为1~n的排列,在将其中一个