POJ 1836.Alignment
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.2Sample Output
4
题意的大概意思就是删去数列中的一些数后,能够保证对于任一个数,其左边严格单调递减或者右边严格单调递增
因此删除后的数列中间两项是可以相等的
首先应该算出对于每一个数,从其向左的递减数列和向右的递增数列
也即,从两头分别算出最长上升子序列(dp[i]是在确保使用上i的情况下的最长上升子序列)
对于dp[i]
若a[i]>a[j]
dp[i]=max{ dp[i] , dp[j]+1 }
否则
dp[i]=max{ dp[i] , 1}
将dp初始化为1,则只需要判断a[i]>a[j]的内容即可
即dp[i]=max{ dp[i] , dp[j]+1 }
最后枚举i、j求出dp1[i]+dp2[j]的最大值,即队伍中剩下的人数
用n减去得到答案
AC代码:GitHub
1 /* 2 By:OhYee 3 Github:OhYee 4 HomePage:http://www.oyohyee.com 5 Email:oyohyee@oyohyee.com 6 Blog:http://www.cnblogs.com/ohyee/ 7 8 かしこいかわいい? 9 エリーチカ! 10 要写出来Хорошо的代码哦~ 11 */ 12 13 14 #include <cstdio> 15 #include <algorithm> 16 #include <cstring> 17 #include <cmath> 18 #include <string> 19 #include <iostream> 20 #include <vector> 21 #include <list> 22 #include <queue> 23 #include <stack> 24 #include <map> 25 using namespace std; 26 27 //DEBUG MODE 28 #define debug 0 29 30 //循环 31 #define REP(n) for(int o=0;o<n;o++) 32 33 const int maxn = 1005; 34 double num[maxn]; 35 int ans[maxn]; 36 37 int dp1[maxn]; 38 int dp2[maxn]; 39 40 //最长上升子序列 41 void LIS(double *a,int n,int *dp) { 42 REP(n) 43 dp[o] = 1; 44 for(int i = 0;i < n;i++) 45 for(int j = 0;j < i;j++) 46 if(a[i] > a[j]) 47 dp[i] = max(dp[i],dp[j] + 1); 48 } 49 void LIS2(double *a,int n,int *dp) { 50 REP(n) 51 dp[o] = 1; 52 for(int i = n-1;i >= 0;i--) 53 for(int j = n-1;j > i;j--) 54 if(a[i] > a[j]) 55 dp[i] = max(dp[i],dp[j] + 1); 56 } 57 58 bool Do() { 59 int n; 60 if(scanf("%d",&n) == EOF) 61 return false; 62 63 REP(n) 64 scanf("%lf",&num[o]); 65 66 //最小上升子序列 67 /* 68 dp[i]为前i个数能生成的最小上升子序列长度 69 对于第i个数有两种情况: 70 71 若a[i]>a[j] 72 dp[i]=max{dp[j]+1,dp[i]} 73 若a[i]<a[j] 74 dp[i]=max{1,dp[i]} 75 76 即dp[i] = max(dp[i],dp[j] + 1) (a[i]>a[j]) 77 初始为1 78 */ 79 80 81 LIS(num,n,dp1); 82 LIS2(num,n,dp2); 83 84 /* 85 int Max = -1; 86 bool flag = false; 87 REP(n) { 88 ans[o] = dp1[o] + dp2[o]; 89 if(Max == ans[o] && ans[o-1] == Max) { 90 flag = true; 91 } else { 92 if(Max < ans[o]) { 93 Max = ans[o]; 94 flag = false; 95 } 96 } 97 } 98 */ 99 100 int ans2 = -1; 101 for(int i = 0;i < n ;i++) 102 for(int j = i + 1;j < n ;j++) 103 ans2 = max(ans2,dp1[i] + dp2[j]); 104 /* 105 REP(n) 106 printf("%d ",dp1[o]); 107 printf("\n"); 108 REP(n) 109 printf("%d ",dp2[o]); 110 printf("\n"); 111 REP(n) 112 printf("%d ",ans[o]); 113 printf("\n"); 114 */ 115 116 //printf("%d\n",n - (Max - 1) - flag); 117 printf("%d\n",n - ans2); 118 119 return true; 120 } 121 122 int main() { 123 while(Do()); 124 return 0; 125 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com