题面:
回顾下最长公共子序列:
if(a[i]!=b[j])
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
else
dp[i][j] = dp[i-1][j-1] + 1;
复杂度为O(n^2)
但是这题不行,数据卡到了1e5,所以应该再次观察:
注意到是两个全排列,那么利用map,把第一个序列当作基准列,做等效替换:把原来的值替换成1,2,3...n,然后对第二个序列用LIS,降到nlogn
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef unsigned long long ll;
using namespace std;
const int N = 1e5 + 5;
int n;
map<int, int>mp;
int a[N], val[N];
int binsearch(int l, int r, int va)//二分查找
{
int ll = l, rr = r, mid;
while (ll < rr)
{
mid = (ll + rr) >> 1;
if (va > val[mid])ll = mid + 1;
else rr = mid;
}
return ll;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
int xx; cin >> xx;
mp[xx] = i + 1;
}
for (int i = 1; i <= n; i++)
{
cin >> a[i];
val[i] = INT_MAX;//标准的LIS模板
}
val[1] = mp[a[1]];
int ans = 1;
for (int i = 2; i <= n; i++)
{
if (mp[a[i]] > val[ans])
{
ans++; val[ans] = mp[a[i]];
}
else
val[binsearch(1, ans, mp[a[i]])] = mp[a[i]];
}
cout << ans;
return 0;
}
不过谁能想到看似考LCS实则考LIS呢(笑
标签:val,int,ll,P1439,ans,序列,include,模板,mp From: https://www.cnblogs.com/zzzsacmblog/p/18115523