zoukankan      html  css  js  c++  java
  • UVA

                                  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 ≤ 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 · 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

    题意:

      给你N个数的序列,可以删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增子序列,问你最长是多少

    题解:

      我们首先想到的就是枚举 删除哪一段了,最后必定能出现最长

      但是明显是O(n^3)

      我们预处理对于 i 这个数向左向右分别能衍生的长度,这样是O(n^2),还不够

      我们只枚举右端点,不枚举左端点,而是用二分/树状数组找到一个j<i并且a[j]<a[i]的价值最大的a[j];

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    using namespace std;
    const int N = 1e6+7, M = 30005, mod = 1e9+7, inf = 0x3f3f3f3f;
    typedef long long ll;
    //不同为1,相同为0
    
    
    int n,a[N],b[N],righ[N],lef[N],T;
        int C[N],A[N];
        int lowbit(int x) {
            return x&(-x);
        }
        void add(int x, int add) {
            for (; x < N; x += lowbit(x)) {
                C[x] = max(add,C[x]);
            }
        }
        int sum(int x) {
            int s = 0;
            for (; x > 0; x -= lowbit(x)) {
                s = max(C[x],s);
            }
            return s;
        }
    void init() {
        memset(C,0,sizeof(C));
        memset(righ,0,sizeof(righ));
        memset(lef,0,sizeof(lef));
        righ[n] = 1;lef[1] = 1;
        for(int i = n-1;i >= 1;i--){
            if(a[i] >= a[i+1]) righ[i] = 1;
            else righ[i] = righ[i+1] + 1;
        }
        for(int i = 2;i <= n;i++){
            if(a[i] > a[i-1]) lef[i] = lef[i-1] + 1;
            else lef[i] = 1;
        }
       // for(int i=n;i>=1;i--) cout<<righ[i]<<endl;
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
           int ans = 1;
            scanf("%d",&n);
            for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i] = a[i];
            sort(b+1,b+n+1);
            int c = unique(b+1,b+n+1) - b - 1;
            for(int i=1;i<=n;i++) a[i] = lower_bound(b+1,b+c+1,a[i]) - b;
            init();
            add(a[1],lef[1]);
            for(int i=2;i<=n;i++) {
             ans = max(ans,sum(a[i]-1)+righ[i]);
                add(a[i],lef[i]);
            }
             printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    socket server的N种并发模型
    进程、线程以及Goroutine的区别
    分布式从ACID、CAP、BASE的理论推进
    epoll的理论与IO阻塞机制
    golang面试题知识点总结
    golang中如何进行项目模块及依赖管理
    面对golang中defer,要注意什么?
    Kaggle 学习之旅
    推荐在线学习读书网站
    k8s 的 dashboard 的实践
  • 原文地址:https://www.cnblogs.com/zxhl/p/5350412.html
Copyright © 2011-2022 走看看