From 31044bbf610c7ff03f2e8180494c1d69e8943014 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 28 Mar 2022 18:52:52 +0800 Subject: [PATCH] =?UTF-8?q?P1091=20[NOIP2004=20=E6=8F=90=E9=AB=98=E7=BB=84?= =?UTF-8?q?]=20=E5=90=88=E5=94=B1=E9=98=9F=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R72624461 --- Luogu/P1091/P1091.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Luogu/P1091/P1091.cpp diff --git a/Luogu/P1091/P1091.cpp b/Luogu/P1091/P1091.cpp new file mode 100644 index 00000000..e626637e --- /dev/null +++ b/Luogu/P1091/P1091.cpp @@ -0,0 +1,40 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105; + +int n, a[N], f[N], g[N], ans; + +int main() { + std::ios::sync_with_stdio(false); + + 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[i] > a[j]) f[i] = std::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] = std::max(g[i], g[j] + 1); + } + } + + for (int i = 1; i <= n; i++) { + ans = std::max(ans, f[i] + g[i] - 1); + } + + cout << n - ans << endl; + + return 0; +}