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 forti cation 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 ring 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 rst 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 rst 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.
SampleInput
2
9
5 3 4 9 2 8 6 7 1
7
1 2 3 10 4 5 6
Output
4
6

解题思路:首先对数据进行离散化,然后用线段树维护一下即可,水之~~

#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 2 * 100010;
int arr1[maxn], arr2[maxn];
int L[maxn], R[maxn];
int T, n, nn;

int bsearch(int x) {
    int l = 0, r = nn - 1;
    int mid;
    while(l <= r) {
        mid = (l + r) / 2;
        if(arr1[mid] == x) {
            return mid;
        } else if(arr1[mid] < x) {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    return -1;
}

int maxnum[maxn<<2];

void push_up(int rt) {
    maxnum[rt] = max(maxnum[rt<<1], maxnum[rt<<1|1]);
}

void build(int l, int r, int rt) {
    maxnum[rt] = 0;
    if(l == r) return ;
    int m = (l + r) >> 1;
    build(l, m, rt<<1);
    build(m + 1, r, rt<<1|1);
    push_up(rt); ///
    return ;
}

void update(int p, int x, int l, int r, int rt) {
    if(l == r) {
        maxnum[rt] = max(maxnum[rt], x);
        return ;
    }
    /// push_down
    int m = (l + r) >> 1;
    if(p <= m) {
        update(p, x, l, m, rt<<1);
    } else {
        update(p, x, m + 1, r, rt<<1|1);
    }
    push_up(rt);  ///
    return ;
}

int query(int L, int R, int l, int r, int rt) {
    if(L == l && R == r) {
        return maxnum[rt];
    }
    int m = (l + r) >> 1;
    /// push_down
    if(R <= m) {
        return query(L, R, l, m, rt<<1);
    } else if(L > m) {
        return query(L, R, m + 1, r, rt<<1|1);
    } else {
        int t1 = query(L, m, l, m, rt<<1);
        int t2 = query(m + 1, R, m + 1, r, rt<<1|1);
        return max(t1, t2);
    }
}

int main() {

    //freopen("aa.in", "r", stdin);

    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) {
            scanf("%d", &arr1[i]);
            arr2[i] = arr1[i];
        }
        sort(arr1, arr1 + n);
        nn = unique(arr1, arr1 + n) - arr1;
        for(int i = 0; i < n; ++i) {
            arr2[i] = bsearch(arr2[i]) + 1;
        }
        memset(L, 0, sizeof(L));
        memset(R, 0, sizeof(R));
        L[0] = 1;
        R[n-1] = 1;
        for(int i = 1; i < n; ++i) {
            if(arr2[i] > arr2[i-1]) {
                L[i] = L[i-1] + 1;
            } else {
                L[i] = 1;
            }
        }
        for(int i = n - 2; i >= 0; --i) {
            if(arr2[i] < arr2[i+1]) {
                R[i] = R[i+1] + 1;
            } else {
                R[i] = 1;
            }
        }
        /*
        for(int i = 0; i < n; ++i) {
            printf("%d ", L[i]);
        }
        printf("\n");
        for(int i = 0; i < n; ++i) {
            printf("%d ", R[i]);
        }
        printf("\n");
        */
        int ans = R[0];
        build(1, nn, 1);
        update(arr2[0], L[0], 1, nn, 1);
        for(int i = 1; i < n; ++i) {
            int t = 0;
            if(arr2[i] - 1 > 0) {
                t = query(1, arr2[i] - 1, 1, nn, 1);
            }
            ans = max(ans, t + R[i]);
            update(arr2[i], L[i], 1, nn, 1);
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值