482. 合唱队形

摘要
Title: 482. 合唱队形
Tag: LIS
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

482. 合唱队形

  • 题意

    略 (形成山丘式的队形,最少提几个人)

  • 思路

    前后各做一次LIS(必须是dp)
    f[i] 就表示以i为结尾的正序的LIS
    g[i] 就表示以i为结尾的逆序的LIS

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #undef int

    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'

    const int N = 1e5 + 10, INF = 0x3f3f3f3f;

    int a[N], f[N], g[N];

    signed main() {
    //freopen("Tests/input_1.txt", "r", stdin);
    IOS;

    int n;
    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; -- i) {
    g[i] = 1;
    for (int j = n; j > i; -- j)
    if (a[j] < a[i]) g[i] = max(g[i], g[j] + 1);
    }

    int mx = 0;
    for (int i = 1; i <= n; ++ i) {
    mx = max(mx, f[i] + g[i] - 1);
    }

    cout << n - mx;

    return 0;
    }
使用搜索:谷歌必应百度