0分代码求助

B3637 最长上升子序列

Alystkia @ 2023-12-27 21:21:00

#include<iostream>
#include<algorithm>
using namespace std;
int a[1000010];
int main()
{
    int n,ans=1,last=0;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=2;i<=n;i++)
    {
        if(a[i]-1==a[i-1])
        {
            ans++;
        }
        else
        {
            if(ans>=last)
        {
                last=ans;
        }
            ans=1;
        }
    }
    cout<<last<<endl;
    return 0;
}

可能是题木理解错了。


by rnf5114 @ 2023-12-27 21:24:01

@ZengYilin_Lean 是的没错,你理解错了


by Alystkia @ 2023-12-27 21:24:55

@rnfmabj5114 哪里错了?


by Alystkia @ 2023-12-27 21:27:25

谔谔,好像是不用排序的QAQ


by cfm123456 @ 2023-12-27 21:35:18

#include<bits/stdc++.h>
using namespace std;
const int N=5010;
int n;
int a[N],f[N]; 
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    } 
    for(int i=1;i<=n;i++){
        f[i]=1;
        for(int j=1;j<i;j++){
            if(a[j]<a[i]){
                f[i]=max(f[i],f[j]+1);
            }
        }
    } 
    int res=0;
    for(int i=1;i<=n;i++){
        res=max(res,f[i]);
    }    
    cout<<res;
    return 0;   
}

可以用状态转移方程做吧? @ZengYilin_Lean


by Alystkia @ 2023-12-27 21:38:23

@cfm123456 蟹蟹


|