Defense Lines

4 篇文章 0 订阅

题目:

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 ≤ 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 ≤ 2 · 10 5
denoting the number of towers. The second line contains n positive integers not larger than 10 9
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

#include <bits/stdc++.h>
using namespace std;
#define maxN 200000+5
#define INF 2<<29
int lefts[maxN],rights[maxN],dp[maxN],a[maxN],d[maxN];
int main()
{
    int T,n;
    scanf("%d",&T);

    while(T--)
    {
        memset(lefts,0,sizeof(lefts));//存储最长连续字段和
        memset(rights,0,sizeof(rights));//存储答案
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                if(a[i]>a[i-1])
                lefts[i]=lefts[i-1]+1;
                else
                    lefts[i]=1;
            }
              rights[n]=1;
            for(int j=n-1;j>=1;j--)
            {
                if(a[j]<a[j+1])
                    rights[j]=rights[j+1]+1;//j以右的最长连续上升序列
                else
                    rights[j]=1;
            }

            int ans=-1;//接下来找到第i个之前的最长连续上升序列,在logn时间内实现
           fill(dp+1,dp+n+1,INF);//dp[i]表示长度为i的时候序列结尾的最小a值
           for(int i=1;i<=n;i++)
           {
               if(a[i] < dp[lefts[i]]) 
                dp[lefts[i]] = a[i];
               
               int k=lower_bound(dp+1,dp+n+1,a[i])-dp;//找到大于等于a[i]的第一个数
               d[i]=k+rights[i]-1;//以a[i]作为连接点的最长上升序列
               ans=max(ans,d[i]);


           }
        printf("%d\n",ans);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值