Alignment Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 15867 Accepted: 5177
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表示从左到右,用dp1表示从右向左,然后就更新n-dp[i]-dp1[i]+1就可以了,结果一直就是wa。哇的一声就哭了,看了一下别人的题解,惊醒了我原来还有可能等高的两个在一起啊,如果按照我的算法的话,等高的是要算7个人,也就是要删掉一个人,然而实际上并不需要,所以我们就应当设置两个端点,分别记录到这两个端点的最长公共子序列的值,那么,这两个dp值的和就是符合条件的人,然后用n-这两个的和就可以了。列举和的时候每次更新一下最大值,最后就是n-MAXSUM就是答案~
来个图:
题意就可以转化成,令到原队列的最少士兵出列后,使得新队列任意一个士兵都能看到左边或者右边的无穷远处。就是使新队列呈三角形分布就对了,QAQ。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> using namespace std; const int MAXN=1005; const int inf=0x3f3f3f; int n,m; double a[MAXN]; int dp[MAXN],dp1[MAXN]; int main() { int i,j; scanf("%d",&n); for(i=0; i<n; ++i)scanf("%lf",&a[i]); int ans=0; //顺序求最长公共子序列 for(i=0; i<n; ++i) { int mm=0; for(j=0; j<i; ++j) { if(a[j]<a[i]&&dp[j]>mm) { mm=dp[j]; } } dp[i]=mm+1; } //倒着来一遍 for(i=n-1; i>=0; --i) { int mm=0; for(j=n-1; j>i; --j) { if(a[j]<a[i]&&dp1[j]>mm) { mm=dp1[j]; } } dp1[i]=mm+1; } //更新能够符合条件的最大值 for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { ans = max(ans, dp[i]+dp1[j]); } } //输出答案 printf("%d\n", n-ans); return 0; }
