0fen

B3637 最长上升子序列

cinCi @ 2024-07-21 09:41:50

#include<bits/stdc++.h>
#define N 1000020
using namespace std;
using ll=long long;
int dp[N],cnt=0,a[N];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>dp[i];
    }
    for(int i=1;i<n-1;i++){
        if(!((dp[i-1]<dp[i])&&(dp[i]<dp[i+1]))){
            dp[i]=0;
        }
    }
    int kk=0;
    for(int i=0;i<n;i++){
        if(dp[i]){
            kk=max(kk,dp[i]);
        }
    }
    cout<<kk;
    return 0;
}

by Ker_White @ 2024-07-21 09:44:37

牢大膜拜了


by abc1234shi @ 2024-07-21 09:44:42

@cinCi 你算法错了,如果用dp是要这段代码:f[i]=max(f[i],f[j]+1);


by cinCi @ 2024-07-21 09:47:02

@abc1234shi 可是我写的是贪心:)


by abc1234shi @ 2024-07-21 09:48:00

@cinCi 可你用的是dp数组,这题可能dp是比较好吧


by cinCi @ 2024-07-21 09:51:08

@abc1234shi 谁不会dp呀,我只是追求最搞笑的时间复杂度


by cinCi @ 2024-07-21 09:51:22

@abc1234shi qwq


by hhztl @ 2024-07-21 09:51:29

@cinCi 题目要求的是最长上升子序列的长度,但你的代码答案是从 dp 数组里找的,明显不对吧。


by syy999 @ 2024-07-24 11:01:42

me too.

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[1000005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(a,0,sizeof(a));
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        a[x]++;
    }
    int cc=0;
    for(int i=1;i<=1000000;i++){
        if(a[i]) cc++;
    }
    cout<<cc;
    return 0;
}

|