POJ 1836 Alignment
POJ 1836 Alignment(双向LIS)解题报告
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/B
原题:
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.
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]
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
题目大意:
有N个身高不一样的士兵排成一排,现在要去掉一些人,使剩下的每一个士兵都能看到队首或队尾,即 前面或后面没有比他高或和他一样高的人,求,至少要去掉
多少人。
分析:
这一个双向LIS的题目,即最长上升子序列和最长下降子序列,要分别从前往后和从后往前求LIS,但不能直接套用LIS。去掉一些人,使队伍成一个三角形或一个
梯形(最高的两个值相等)。
从前往后:对第i个人求1~i的以i结尾的最长上升子序列的长度
从后往前:对第i个人求n~i的以i结尾的最长上升子序列的长度
总长度就是这两个值相加减1,再用N-最多剩余的长度,即为最少去除的。
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1005; 7 8 int dp1[maxn],dp2[maxn];//dp1[]从前往后LIS;dp2[]从后往前LIS 9 double a[maxn];//身高 10 11 int max(int a,int b) 12 { 13 return a>b?a:b; 14 } 15 16 int main() 17 { 18 int n;//士兵人数 19 while(scanf("%d",&n)!=EOF) 20 { 21 for(int i=1;i<=n;i++) 22 { 23 scanf("%lf",&a[i]); 24 dp1[i]=1;//初始化dp1[] 25 dp2[i]=1;//初始化dp2[] 26 } 27 int ans=0;//剩余士兵的最大长度 28 for(int i=1;i<=n;i++)//求最长上升子序列dp1[] 29 { 30 for(int j=1;j<=i-1;j++) 31 { 32 if(a[j]<a[i]) 33 dp1[i]=max(dp1[i],dp1[j]+1); 34 } 35 } 36 for(int i=n;i>=1;i--)//求最长下降子序列dp2[] 37 { 38 for(int j=i+1;j<=n;j++) 39 { 40 if(a[j]<a[i]) 41 dp2[i]=max(dp2[i],dp2[j]+1); 42 } 43 } 44 for(int i=1;i<=n;i++)//判断最高点 45 { 46 int flag=1;//有无最高点标识 47 for (int j=i-1;j>=1;j--) 48 { 49 if(a[j]==a[i])//找到i前面的离i最近的与a[i]相等的点,更新dp1[i] 50 { 51 dp1[i]=max(dp1[i],dp1[j]+1); 52 flag=0; 53 break; 54 } 55 } 56 for(int j=i+1;j<=n;j++) 57 { 58 if(flag=1&&a[j]==a[i])//先判断前面有没有算过最高点(a==0),若没算过,找到i后面的离i最近的与a[i]相等的最高点,更新dp2[i] 59 { 60 dp2[i]=max(dp2[i],dp2[j]+1); 61 break; 62 } 63 } 64 ans=max(ans,dp1[i]+dp2[i]-1);//更新最大长度 65 } 66 printf("%d\n",n-ans);//最小删除的长度 67 } 68 return 0; 69 }