P1091 [NOIP2004 提高組] 合唱隊形

Aloongyy發表於2024-10-19

分析題目知道求一個最長上升子序列和一個最長下降子序列的長度均為同一個起點的最長的長度。於是求

點選檢視程式碼
#include <iostream>
#include <stack>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <climits>
#include <string.h>
#include <map>
#include <queue>
#include <list>
#include <cmath>
#include <iomanip> 
#define int long long 
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc u<<1
#define rc u<<1|1
#define gcd __gcd
#define double long double
#define endl "\n"
#define INF LLONG_MAX
#define mod  998244353 
#define N 105
const double PI = 3.14159265358979323846;
using namespace std;
int n, t[N], a[N], b[N];//分別表示以該數結尾最長子序列的長度
signed main()
{
    ios;
    int n; cin >> n;
    for (int i = 1; i <= n; i++)cin >> t[i];
    for (int i = 1; i <= n; i++)
    {
        a[i] = 1;
        for (int j = 1; j < i; j++)
        {
            if (t[i] > t[j])a[i] = max(a[i], a[j] + 1);
            else continue;
        }
    }
    for (int i = n; i >=1; i--)
    {
        b[i] = 1;
        for (int j = n; j > i; j--)
        {
            if (t[i] > t[j])b[i] = max(b[i], b[j] + 1);
            else continue;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        //cout << a[i] << " " << b[i] << endl;
        ans = max(a[i] + b[i] - 1, ans);
    }
    cout << n - ans << endl;
    return 0;
}

相關文章