POJ - 1836 Alignment (最长上升子序列 O(nlogn))

64 篇文章 0 订阅
该博客探讨了POJ 1836题目的解题思路,强调了如何在保持士兵视线畅通的情况下,确定最少需要出列的士兵数量。博主指出,问题可以转换为寻找最长不降子序列和最长不升子序列,并且解释了为何等高士兵也可能影响视线。解决方案不需要特殊处理某些特殊情况,如等高士兵相邻,而是聚焦于序列的递增和递减部分。
摘要由CSDN通过智能技术生成


解题思路:是POJ2533的扩展题。

题意不难,令到原队列的最少士兵出列后,使得新队列任意一个士兵都能看到左边或者右边的无穷远处。就是使新队列呈三角形分布就对了。

 

但这里有一个陷阱,看了一些别人的解题报告说“任一士兵旁边不能存在等高的士兵”,然后又举了一个例子说注意

3

5 5 5

的情况,我没看他们的程序,不知道他们是不是把这些例子特殊处理了,但完全没必要,因为“允许处于三角形顶部的两个士兵等高”,图形化就是如下图:


 

其实蓝色士兵的身高和红色士兵的身高是完全没有关系的。

 

要求最少出列数,就是留队士兵人数最大,如图,即左边的递增序列人数和右边的递减序列人数之和最大

因而可转化为求“最长不降子序列”和“最长不升子序列”问题



注意:等高的也会遮住视线!!!!


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double inf = 3.0;
int pre[1111],back[1111];
double dp[1111],a[1111];
void print(int n)
{
    int i;
    for(i=1;i<=n;i++) cout<<pre[i]<<" ";
    cout<<endl;
    for(i=1;i<=n;i++) cout<<back[i]<<" ";
    cout<<endl;
}
int main()
{
    int i,j,n,ans;
    int p1,p2,p;
    cin>>n;
    for(i=1;i<=n;i++) cin>>a[i];
    fill(dp,dp+n,inf);
    for(i=1;i<=n;i++) {
        p = lower_bound(dp,dp+n,a[i])-dp;
        dp[p] = a[i];
        pre[i] = max(pre[i-1],p+1);
    }
    fill(dp,dp+n,inf);
    ans=0;
    for(i=n;i;i--) {
        p = lower_bound(dp,dp+n,a[i])-dp;
        dp[p] = a[i];
        back[i] = max(back[i+1],p+1);
    }
  //  print(n);
    for(i=1;i<=n;i++) {
    	for(j=i+1;j<=n;j++) {
    		ans=max(ans,pre[i]+back[j]);
		}
	}

    cout<<n-ans<<endl;
    return 0;
}


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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值