求助,86分
查看原帖
求助,86分
1115391
zxbsdkk9468楼主2024/11/6 12:33
#include<bits/stdc++.h>
//取消同步流会让cout输出得比printf快
using namespace std;
#define lowbit(x) (x&(-x))
using ll = long long;
vector<int>e[100];
char mp[1100][1100];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
const ll P=1e9+7;
const int N=1e5+100;
ll a[N];
ll g[N];
ll f[N];
ll gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
ll che(ll v,ll n)
{
    int l=1;
    int r=n;
    while(l<r)
    {
        ll mid=(l+r)>>1;
        if(g[mid]<=v)
        {
            r=mid;
        }
        else l=mid+1;
    }
    return l;
}
void solve() {
   int i=0;
    while(cin>>a[++i])
    {
    }
    int n=i;
    int cnt=1;
    //最长单调不升子序列
    g[0]=1e9;
    g[1]=a[1];
    int k=1;
    for(int i=1;i<=n;i++)
    {
        if(a[i]<=g[cnt])
        {
            g[++cnt]==a[i];
        }
        else 
        {
            int k=che(a[i],cnt);
            g[k]=a[i];
        }
    }
    cout<<cnt<<"\n";
    //最长上升子序列
    k=1;
    cnt=1;
    f[1]=a[1];
    for(int i=1;i<=n;i++)
    {
        if(a[i]>f[cnt])
        {
            f[++cnt]=a[i];
        }
        else {
            k=lower_bound(f,f+cnt+1,a[i])-f;
            f[k]=a[i];
        }
    }
    cout<<cnt<<"\n";
}
int main() 
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t=1;
    while (t--) {
        solve();
    }
    return 0;
}
2024/11/6 12:33
加载中...