1 条题解

  • 0
    @ 2022-12-25 15:39:07
    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int a[105];
    int f[105]; // f[i]:从前往后、以a[i]结尾的LIS长度
    int g[105]; // g[i]:从后往前、以a[i]结尾的LIS长度
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        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);
        }
        for (int i = n; i >= 1; i--)
        {
            g[i] = 1;
            for (int j = n; j > i; j--)
                if (a[i] > a[j])
                    g[i] = max(g[i], g[j] + 1);
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans = max(ans, f[i] + g[i] - 1);
        cout << n - ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    483
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    (无)
    递交数
    23
    已通过
    16
    上传者