uva 1471 - Defense Lines (最长上升子序列变形)

After the last war devastated your country, you - as the king of the land of Ardenia - decided it was high time to improve the defense of your capital city. A part of your fortification is a line of mage towers, starting near the city and continuing to the northern woods. Your advisors determined that the quality of the defense depended only on one factor: the length of a longest contiguous tower sequence of increasing heights. (They gave you a lengthy explanation, but the only thing you understood was that it had something to do with firing energy bolts at enemy forces).

After some hard negotiations, it appeared that building new towers is out of question. Mages of Ardenia have agreed to demolish some of their towers, though. You may demolish arbitrary number of towers, but the mages enforced one condition: these towers have to be consecutive.

For example, if the heights of towers were, respectively, 5, 3, 4, 9, 2, 8, 6, 7, 1, then by demolishing towers of heights 9, 2, and 8, the longest increasing sequence of consecutive towers is 3, 4, 6, 7.

Input

The input contains several test cases. The first line of the input contains a positive integer Z$ \le$25, denoting the number of test cases. Then Z test cases follow, each conforming to the format described below.

The input instance consists of two lines. The first one contains one positive integer n$ \le$2 . 105 denoting the number of towers. The second line contains n positive integers not larger than 109 separated by single spaces being the heights of the towers.

Output

For each test case, your program has to write an output conforming to the format described below.

You should output one line containing the length of a longest increasing sequence of consecutive towers, achievable by demolishing some consecutive towers or no tower at all.

Sample Input

2 
9 
5 3 4 9 2 8 6 7 1 
7 
1 2 3 10 4 5 6

Output

4 
6

首先纠正一处书上翻译遗漏,可以不删除任何子序列
这道题是要求两个连续子序列拼出来的最大值。最简单的想法是以O(n)的复杂度预处理出,以i点为最右端的最长连续上升序列的长度 left[i]和i点为最左端的right[i]
然后两两拼接,这样处理的复杂度是n^2,从数据范围来看,我们是需要优化到nlogn的,怎么优化呢?
预处理是已经不能优化了,然后想对于每个i,在找在他前面能和它拼接的串的时候我们需要一一枚举吗?
我们回顾一下求最长上升子序列的nlogn算法,它在找能和第i个点拼接的方式是二分搜索:
就是用d[i]表示以i结尾的最长上升序列的长度,g[i]表示d值为i的最小最小状态编号,可以推出g是单调递增的,所以可以二分搜索。
对于这道题,是求连续上升序列,还可以用上面的方法吗?可以,因为这题也可以满足上面二分搜索的前提,那么关键就是这道题的g值怎么维护了
其实这个g值的维护,比最长上升序列的那个g值的维护简单,但感觉这里表述起来比较麻烦,用代码说明吧
if(a[i] < g[left[i]]) g[left[i]] = a[i];

这题算作LIS问题的变形,关键是需要理解LIS的nlogn解法的那个二分搜索,然后灵活运用到这个题中。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 200000 + 5;
const int INF = 1000000000;

int a[maxn],g[maxn],d[maxn];
int right[maxn],left[maxn];

int main(){
    int t,n,ans;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i = 0;i < n;i++)
            scanf("%d",&a[i]);
        memset(right,0,sizeof(right));
        memset(left,0,sizeof(left));
        right[n-1] = 1;left[0] = 1;
        for(int i = n-2;i >= 0;i--){
            if(a[i] >= a[i+1]) right[i] = 1;
            else right[i] = right[i+1] + 1;
        }
        for(int i = 1;i <= n-1;i++){
            if(a[i] > a[i-1]) left[i] = left[i-1] + 1;
            else left[i] = 1;
        }
        ans = -1;
        for(int i = 1;i <= n;i++) g[i] = INF;
        for(int i = 0;i < n;i++){
            int k = lower_bound(g+1,g+n+1,a[i]) - g;
            d[i] = k + right[i] - 1;
            if(a[i] < g[left[i]]) g[left[i]] = a[i];
            ans = max(ans,d[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值