防线(Defense Lines,ACM/ICPC CERC 2010 UVa1471)

题目大意:

给一个长度为n(n<=200000)的序列,你的任务时删除一个连续子序列,使得剩下的序列中有一个长度最大的连续增子序列。

预处理得到g[i](表示a[i]结尾的最长连续增序列)f[i](表示a[i]开头的最长连续增序列),用set存a[i],g[i],然后通过f[i]找最优的a[j],g[j]。

// UVa1471 Defense Lines
// Rujia Liu
// Algorithm 1: use STL set to maintain the candidates.
// This is a little bit more intuitive, but less efficient (than algorithm 2)
#include<cstdio>
#include<set>
#include<cassert>
using namespace std;

const int maxn = 200000 + 5;
int n, a[maxn], f[maxn], g[maxn];

struct Candidate {
  int a, g;
  Candidate(int a, int g):a(a),g(g) {}
  bool operator < (const Candidate& rhs) const {
    return a < rhs.a;
  }
};

set<Candidate> s;

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
      scanf("%d", &a[i]);
    if(n == 1) { printf("1\n"); continue; }

    g[0] = 1;
    for(int i = 1; i < n; i++)///得到g
      if(a[i-1] < a[i]) g[i] = g[i-1] + 1;
      else g[i] = 1;

    f[n-1] = 1;
    for(int i = n-2; i >= 0; i--)///得到f
      if(a[i] < a[i+1]) f[i] = f[i+1] + 1;
      else f[i] = 1;

    s.clear();
    s.insert(Candidate(a[0], g[0]));//初始化s
    int ans = 1;//开始时字串的长度为1
    for(int i = 1; i < n; i++) {
      Candidate c(a[i], g[i]);

      // 找到s中第一个第一个大于或等于c.a的位置

      set<Candidate>::iterator it = s.lower_bound(c); 
      bool keep = true;
      //用于标记c是否满足进入后面考虑情况的条件
      //存在小于它的数时,如果c.g更小,那么就没有必要考虑,也就不满足条件

      if(it != s.begin()) {
        Candidate last = *(--it); //找到离它最近的一个小于它的数
        int len = f[i] + last.g; 
        ans = max(ans, len);
        if(c.g <= last.g) keep = false;
      }

      if(keep) {
        s.erase(c); // 删除原来那个c.a的那个
        s.insert(c);//插入新的c
        it = s.find(c); 
        it++;
        //在c位置后面的数种删除不满足条件的数据
        while(it != s.end() && it->a > c.a && it->g <= c.g) s.erase(it++);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

//代码来自https://github.com/aoapc-book/aoapc-bac2nd

 

转载于:https://www.cnblogs.com/ke-yi-/p/10175811.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值