萌新刚学dp,求救ww(80pts)

B3637 最长上升子序列

Lovely_Doggie @ 2024-07-01 20:48:22

帮帮我可以吗ww

#include<bits/stdc++.h>
using namespace std;
long long n;
int a[100005],dp[100005],ans=-114514191;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]; 
        dp[i]=1;
    }
    dp[1]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n-1;j++)
        {
            if(a[j]>a[i])
            {
                dp[j]=max(dp[i]+1,dp[j]);
                ans=max(ans,dp[j]);
                // for(int i=1;i<=n+1;i++)
                // {
                //  cout<<dp[i]<<' '; 
                // }
                // cout<<endl;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

by Lovely_Doggie @ 2024-07-01 20:48:59

WA on #1,#3


by qnqfff @ 2024-07-01 20:54:31

#include<bits/stdc++.h>
using namespace std;
long long n;
int a[100005],dp[100005],ans=-114514191;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]; 
        dp[i]=1;
    }
    dp[1]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            if(a[j]>a[i])
            {
                dp[j]=max(dp[i]+1,dp[j]);
                ans=max(ans,dp[j]);
                // for(int i=1;i<=n+1;i++)
                // {
                //  cout<<dp[i]<<' '; 
                // }
                // cout<<endl;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

by Lovely_Doggie @ 2024-07-01 21:00:05

@qnqfff thx


by Union_Find @ 2024-07-01 21:01:23

一开始加一句

for(int i=1;i<=n;i++)
{
dp[i] = 1;
}

比如说 5 4 3 2 1,你之后的数字因为前面没有比他小的数,又没有初始化, dp[i] 就都是 0 了,但实际上自己单独一个也可以。


by huoboran @ 2024-07-01 22:04:12

你帮了我,我也帮帮你吧

   #include <iostream>
#define AUTHOR "HEX9CF"
using namespace std;
using ll = long long;

const int N = 1e6 + 7;

int n;
int a[N];
ll dp[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        dp[i] = 1;
        for (int j = 1; j < i; j++) {
            if (a[j] < a[i]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << "\n";
    return 0;
}

by huoboran @ 2024-07-01 22:04:50

绝对不wa,我过了了。


by huoboran @ 2024-07-01 22:17:11

@Lovely_Doggie cf757b 代码放下面了,去做吧

  #include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int cou[N];

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i) {
        int x;
        scanf("%d", &x);

        for (int j = 1; j * j <= x; ++j) {
            if (x % j == 0) {
                ++cou[j];
                if (j * j != x) {
                    ++cou[x / j];
                }
            }
        }
    }

    int ans = 1;
    for (int i = 2; i < N; ++i) {
        ans = max(ans, cou[i]);
    }

    printf("%d", ans);
}

绝对不WA


by ATION001 @ 2024-07-02 09:28:04

@qnqfff @huoboran 能帮我找下为啥TLE吗?(dp写法)

代码


#include<bits/stdc++.h>
using namespace std;
const int N=5010;
int dp[N][N];//dp[i][j]表示以a[i]结尾长度为j的上升自学了是否存在 
int n,a[N];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],dp[i][1]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<i;k++)
            if(a[k]<a[i])
            dp[i][j]|=dp[k][j-1];
        }
    }
    for(int j=n;j>=1;j--)
    {
        for(int i=1;i<=n;i++)
        {
            if(dp[i][j])
            {
                cout<<j<<'\n';
                return 0;
            }
        }
    }
    return 0;
}

|