poj 1836 LIS变形

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 17331   Accepted: 5694

Description

In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.

Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.

Input

On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).

There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]

Output

The only line of output will contain the number of the soldiers who have to get out of the line.

Sample Input

8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4

Source

 
  就是一道求LIS的变形题目,注意到浮点数并不大,可以乘以1e5之后转化为整数来操作。
  由于和以前做过的一道题类似,不同的是最高的人不一定只出现一次。题目要求的时只要左边或者右边没有比他高的人就好了,并不是绝对的高低限制。可以枚举每个人作为最高的计算N次,假设i是最高的一个单位,我们计算以i为最后一个点之前的最长子序列,在计算i后面去掉大于ai的最长下降子序列加在一起就是队里的人数。
    对于每次枚举出的最高的人,这个身高可以出现两次,一个左一个右就好了。
 
 1 #include <iostream>
 2 #include<algorithm>
 3 #include<stack>
 4 #include<cstdio>
 5 #include<cstring>
 6 using namespace std;
 7 #define inf 0x3f3f3f3f
 8 typedef long long LL;
 9 const int MAX = 1005;
10 int a[MAX], b[MAX], g[MAX];
11 int solve(int m,int n)
12 {
13     memset(g, inf, sizeof(g));
14     int i, j, k1=0, k2=0;
15     for (i = 1;i < m;++i)
16     {
17         int k = lower_bound(g,g+n+1,a[i])-g;
18         g[k] = a[i];
19     }k1 = lower_bound(g, g + n+1, a[m]) - g+1;
20     memset(g, inf, sizeof(g));
21     for (i = 1;i <=n-m;++i)
22     {
23         if (b[i] > a[m]) continue;
24         int k = lower_bound(g, g + n+1, b[i]) - g;
25         g[k] = b[i];
26         k2 = max(k2,k+1);
27     }
28     //printf("%d %d
", k1, k2);
29     return n - (k1 + k2);
30 }
31 int main()
32 {
33     int N, i, j, k ;
34     double d;
35     while (cin >> N) {
36         int ans = inf;
37         for (i = 1;i <= N;++i)scanf("%lf", &d), a[i] = b[N + 1 - i] = d * 100000;
38         for (i = 1;i <= N;++i) {
39             ans = min(ans, solve(i, N));
40         }
41         printf("%d
", ans);
42     }
43     return 0;
44 }
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/zzqc/p/7417607.html