思路
先写一个暴力,给出伪代码:
i->n
j->i-1
if(a_i*a_j<=x)
dp_i=max(dp_i,dp_j+1)我们输出最大的 即可,时间复杂度为 。
考虑优化,分讨原来的情况:
- 当 为 时,显然在 的情况下 永远没有配对的 所以 为 ,反之 就是最大的 。
- 当 时,推导得出满足情况的 必须满足 。
- 当 时,推导得出满足情况的 必须满足 。
然后我们写个离散化 + 线段树来快速确认维护每个值对应的最大 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
template<typename T>inline void read(T&x) {
x = 0;
char c;
int sgin = 1;
do {
c = getchar_unlocked();
if (c == '-')sgin = -1;
} while (!isdigit(c));
do {
x = (x << 3) + (x << 1) + (c & 15);
c = getchar_unlocked();
} while (isdigit(c));
x *= sgin;
}
void write(int x) {
static int sta[40];
int top = 0;
do {
sta[top++] = x % 10;
x /= 10;
} while (x);
while (top) putchar('0' + sta[--top]);
putchar('\n');
}
int n,x;
int a[N],dp[N];
vector<int> v;
struct SGT{
struct node{
int l,r,mx;
}t[N<<3];
void pushup(int p){
t[p].mx=max(t[p<<1].mx,t[p<<1|1].mx);
}
void build(int p,int l,int r){
t[p].l=l,t[p].r=r,t[p].mx=0;
if(l==r) return;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void update(int p,int pos,int val){
if(t[p].l==t[p].r){
t[p].mx=max(t[p].mx,val);
return;
}
int mid=(t[p].l+t[p].r)>>1;
if(pos<=mid) update(p<<1,pos,val);
else update(p<<1|1,pos,val);
pushup(p);
}
int query(int p,int l,int r){
if(l>t[p].r||r<t[p].l) return 0;
if(l<=t[p].l&&r>=t[p].r) return t[p].mx;
int mid=(t[p].l+t[p].r)>>1;
int res=0;
if(l<=mid) res=max(res,query(p<<1,l,r));
if(r>mid) res=max(res,query(p<<1|1,l,r));
return res;
}
}st;
int id(int val){
return lower_bound(v.begin(),v.end(),val)-v.begin()+1;
}
signed main(){
read(n);
read(x);
for(int i=1;i<=n;i++){
read(a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int m=v.size();
st.build(1,1,m);
int ans=0;
for(int i=1;i<=n;i++){
dp[i]=1;
if(a[i]==0){
if(x>=0) dp[i]=max(dp[i],st.query(1,1,m)+1);
}
else if(a[i]>0){
long double lim=(long double)x/a[i];
int pos=upper_bound(v.begin(),v.end(),lim)-v.begin();
if(pos>0) dp[i]=max(dp[i],st.query(1,1,pos)+1);
}
else{
long double lim=(long double)x/a[i];
int bound=ceil(lim);
int pos=lower_bound(v.begin(),v.end(),bound)-v.begin()+1;
if(pos<=m) dp[i]=max(dp[i],st.query(1,pos,m)+1);
}
ans=max(ans,dp[i]);
st.update(1,id(a[i]),dp[i]);
}
write(ans);
return 0;
}