| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 15021 | Accepted: 4884 |
Description
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
Input
There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]
Output
Sample Input
8 1.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
#include <cstdio>
#include <iostream>
using namespace std;
//有两种情况最后1 2 3 2 1 or 1 2 3 3 2 1
int main()
{
int n, up[1005], down[1005];
double h[1005];
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lf", &h[i]);
for (int i = 1; i <= n; i++) { //求从左往右的每个位置的LIS
int maxn = 0;
for (int j = 1; j < i; j++) {
if (h[j] < h[i]) {
if (up[j] > maxn) maxn = up[j];
}
}
up[i] = maxn + 1;
}
for (int i = n; i >= 1; i--) { //求从右往左的每个位置的LIS
int maxn = 0;
for (int j = n; j > i; j--) {
if (h[j] < h[i]) {
if (down[j] > maxn) maxn = down[j];
}
}
down[i] = maxn + 1;
}
int temp = 0;
for (int i = 1; i <= n - 1; i++) { //注意处理第二种情况
for (int j = i + 1; j <= n; j++) {
if (up[i] + down[j] > temp)
temp = up[i] + down[j];
}
}
printf("%d\n", n - temp);
return 0;
}
316



被折叠的 条评论
为什么被折叠?



