Alignment
http://poj.org/problem?id=1836
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16118 Accepted: 5264
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
题意:
这道题的题意其实是很好理解的,只是我这几天水题刷多了,理解了无数次才看懂题意,报应啊(雾。
对于一个序列(double),可以去掉某些数字,使得对于该序列的每一个数字,都比其左边或右边的数字大,求最少要去掉的数字个数。
其实这就是一个LIS的简单变形。正着求一边LIS,再倒着来一遍,最后枚举每一个数为最大值,求其最小值。
代码
#include <cstdio>
#include <iostream>
using namespace std;
int n;
double h[1005];
double a[1005],b[1005];
int l[1005],r[1005];
int main(){
cin >> n;
for(int i = 0;i < n;++i) cin >> h[i];
int cnt1 = 0;
int cnt2 = 0;
for(int i = 0,j = n - 1;i < n;++i,--j){
if(a[cnt1] < h[i]) a[++cnt1] = h[i];
else {
int l = 1,r = cnt1,mid;
while(l < r){
mid = (l + r) >> 1;
if(a[mid] < h[i])l = mid + 1;
else r = mid;
}
a[l] = h[i];
}
if(b[cnt2] < h[j]) b[++cnt2] = h[j];
else {
int l = 1,r = cnt2,mid;
while(l < r) {
mid = (l + r) >> 1;
if(b[mid] < h[j]) l = mid + 1;
else r = mid;
}
b[l] = h[j];
}
l[i] = cnt1;
r[j] = cnt2;
}
int ans = 0;
for (int i = 1;i < n;++i) ans = max(ans,l[i-1]+r[i]);
cout << n - ans;
return 0;
}