跳到正文

题解

题解:P15278 [MCO 2025] Subsequence

Fine_Dust_Z约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年4月15日
收录
2026年8月18日
更新
2026年11月14日
篇幅
约 6 分钟
获赞
1
题目
P15278

版本

  1. v1当前

思路

先写一个暴力,给出伪代码:

代码
i->n
  j->i-1
    if(a_i*a_j<=x)
      dp_i=max(dp_i,dp_j+1)

我们输出最大的 dpidp_i 即可,时间复杂度为 O(N2)O(N^2)。

考虑优化,分讨原来的情况:

  • 当 aia_i 为 00 时,显然在 X<0X < 0 的情况下 aia_i 永远没有配对的 aja_j 所以 dpidp_i 为 11,反之 dpidp_i 就是最大的 dpj+1dp_j + 1。
  • 当 ai<0a_i < 0 时,推导得出满足情况的 aja_j 必须满足 aj≥Xaia_j \ge \frac{X}{a_i}。
  • 当 ai>0a_i > 0 时,推导得出满足情况的 aja_j 必须满足 aj≤Xaia_j \le \frac{X}{a_i}。

然后我们写个离散化 + 线段树来快速确认维护每个值对应的最大 dpjdp_j。

code

C++
#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;
}
原文