POJ1836--Alignment

来源:互联网 发布:Excel对数据进行格式化 编辑:程序博客网 时间:2024/06/05 00:50

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

81.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4
/*此题可以可以转换为求一个点,使得以该点为分界,左边的LIS和右边的LDS之和最大。枚举分界点复杂度为n.不能用朴素算法求LIS和LDS。否则复杂度将达到n^3.应该用复杂度为n*logn的来求LIS和LDS。n的上限是1000注意两个二分查找有略微的区别。在写二分的时候可以先手动举例二分查找下*/#include <iostream>#include <cstdio>using namespace std;float A[1006];float maxg[1006];float ming[1006];int d[1006];int binary_search1(int l,int r,float key){while(l<r){int mid=(l+r)/2;if(maxg[mid]>=key){r=mid;}else l=mid+1;}return l;}int binary_search2(int l,int r,float key){while(l<r){int mid=(l+r)/2;if(ming[mid]<=key){r=mid;}else l=mid+1;}return l;}int main(){int n;while(scanf("%d",&n)!=EOF) {for(int i=1;i<=n;i++){scanf("%f",&A[i]);}//接下来枚举分界点int maxlen=0;for(int i=1;i<=n;i++){int nlen=0;//现在分界点是i.求1到i的LIS和i+1到n的LDS之和for(int j=1;j<=n;j++){ming[j]=-1.0;maxg[j]=100000.0;//初始化}int max1=0;//用来记录最长上升子序列for(int j=1;j<i;j++)//在前i个中最长上升子序列{int k=binary_search1(1,n,A[j]);d[j]=k;if(k>max1)max1=k;maxg[k]=A[j];}nlen+=max1;int max2=0;//用来记录最长下降子序列for(int j=i;j<=n;j++){int k=binary_search2(1,n,A[j]);d[j]=k;if(k>max2)max2=k;ming[k]=A[j];}nlen+=max2;if(nlen>maxlen)maxlen=nlen;}cout<<n-maxlen<<endl;}return 0;}