ABC393F 题解
chenxi2009 · · 题解
思路
一眼离线,先按
考虑我们使用
离线处理,时间复杂度
代码
纯净版求 LIS 代码(供参考)
#include<bits/stdc++.h>
using namespace std;
int n,a[300000],f[300000],ans;
int main(){
memset(f,0x3f,sizeof(f)),f[0] = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i ++) scanf("%d",&a[i]);
for(int i = 1;i <= n;i ++){
x = lower_bound(f + 1,f + n + 1,a[i]) - f - 1;//a_i 可以接在最长长 x 的序列后
f[x + 1] = min(f[x + 1],a[i]),ans = max(ans,x + 1);//接上后长度为 x+1
}
printf("%d",ans);
return 0;
}
本题代码
#include<bits/stdc++.h>
using namespace std;
struct node{
int r,x,w;
bool friend operator < (const node &a,const node &b){
return a.r < b.r;
}
};
int n,q,a[300000],ans[300000],it = 1,f[300000],x;
node qry[300000];
int main(){
memset(f,0x3f,sizeof(f)),f[0] = 0;
scanf("%d%d",&n,&q);
for(int i = 1;i <= n;i ++) scanf("%d",&a[i]);
for(int i = 1;i <= q;i ++) scanf("%d%d",&qry[i].r,&qry[i].x);
for(int i = 1;i <= q;i ++) qry[i].w = i;
sort(qry + 1,qry + q + 1);//按 R 排序
for(int i = 1;i <= n;i ++){
x = lower_bound(f + 1,f + n + 1,a[i]) - f - 1;
f[x + 1] = min(f[x + 1],a[i]);//求 LIS 部分
while(qry[it].r == i){
ans[qry[it].w] = upper_bound(f + 1,f + n + 1,qry[it].x) - f - 1;//二分找第一个 i 使 f_i<=X_it
it ++;
}
}
for(int i = 1;i <= q;i ++) printf("%d\n",ans[i]);
return 0;
}