标签:clu ram contain 思路 空间 时间 mat number present
id=1836
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
那么我们能够分别求出这列数从左到右和从右到左的最长递增子序列。然后得到从中间到两端递减的最长序列所以此与POJ 2533类似。结果即为n减去这个最长序列的长度。
复杂度分析——时间复杂度:O(n^2),空间复杂度:O(n)
附上AC代码:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip> #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; const double pi = acos(-1.0); const double e = exp(1.0); const int maxn = 1005; int prev[maxn], next[maxn]; // 分别代表从左、右边開始的LIS长度 double high[maxn]; // 士兵的身高 int main() { ios::sync_with_stdio(false); int n; while (scanf("%d", &n) != EOF) { for (int i=0; i<n; i++) scanf("%lf", &high[i]); prev[0] = next[n-1] = 1; for (int i=1; i<n; i++) { // 从左边求LIS长度 prev[i] = 1; for (int j=0; j<i; j++) if (high[j] < high[i]) prev[i] = max(prev[i], prev[j]+1); } for (int i=n-2; i>=0; i--) { // 从右边求LIS长度 next[i] = 1; for (int j=n-1; j>i; j--) if (high[j] < high[i]) next[i] = max(next[i], next[j]+1); } int ans = 0; for (int i=0; i<n-1; i++) for (int j=i+1; j<n; j++) ans = max(ans, prev[i]+next[j]); printf("%d\n", n-ans); } return 0; }
标签:clu ram contain 思路 空间 时间 mat number present
原文地址:http://www.cnblogs.com/mfmdaoyou/p/7135861.html