problem
长为 的排列 。
提取两个可以不连续的子序列 ,满足:
求 。。
solution 1
考虑拎出所有的 ,因为是排列,所以是调和级数, 只有 对。
那么我们要选出若干对 使得 分别单调,且不重复。
忽略重复的限制,我们可以先对着 排序,再对 做最长上升子序列,同时满足了两个限制。
为了避免选到相同的 ,对于 ,我们可以将 倒序排序,也就是使 。由于是上升子序列,因此不会选到相同的 了。
code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n,cnt,a[200010],p[200010],c[200010<<4];
pair<int,int> b[200010<<4];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1,x;i<=n;i++) scanf("%d",&x),p[x]=i;
for(int i=1;i<=n;i++){
for(int j=a[i];j<=n;j+=a[i]){
b[++cnt]={i,p[j]};
}
}
sort(b+1,b+cnt+1,[](pair<int,int> a,pair<int,int> b){
return a.second!=b.second?a.second<b.second:a.first>b.first;
});
int top=0;
for(int i=1;i<=cnt;i++){
if(!top||c[top]<b[i].first) c[++top]=b[i].first;
else *lower_bound(c+1,c+top+1,b[i].first)=b[i].first;
}
printf("%d\n",top);
return 0;
}
solution 2
考虑暴力 DP。
优化之。滚掉 ,逐个加入 ,用 更新 :
由于满足 的数量是调和级数,因此只有 的枚举量。
对于 ,线段树维护之。总的复杂度 。