CUSOJ1170--A Simple Problem

这个题目如果直接暴力枚举连续子区间是肯定会超时的;其实枚举子区间的时候会有很多重复的计算,我们可以利用这个性质.假设我们现在我们枚举起点为i,终点为j-1的区间是满足要求的,但是由于a[j]的值大于最大值或小于最小值,导致Max-Min>k,那么如果我们是暴力枚举的话,我们会将起点设为i+1然后继续枚举,但是其实如果a[i+1]点的值没有改变最大值或最小值则区间的终点仍然是j,不会得到一个更优的解.所以我们下次枚举的起点应该是第一个改变了最大值/最小值而使得当前区间Max-Min<=k的坐标.

要快速的确定这个坐标是这个题目的关键所在;开始的时候我用线段数保存区间最小值,最大值,然后用二分的方法进行查询,但是这样超时了.后面发现这个题目不需要修改只需要查询.所以我们可以直接用线段树查询到最大最小值的坐标.

PS:后面在网上看到可以用单调队列实现上面的功能.

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

#define ll long long

int  const maxn=1100000;
ll   Min[maxn*4],Max[maxn*4],a[maxn];
ll   n,k;

void Build(ll  rt,ll  l,ll  r)
{
       if(l>=r)
       {
            Max[rt]=l;
            Min[rt]=r;
            return ;
       }
       ll  m=(l+r)>>1;
       Build(rt<<1,l,m);
       Build(rt<<1|1,m+1,r);
       Max[rt]=a[Max[rt<<1]]>a[Max[rt<<1|1]]?Max[rt<<1]:Max[rt<<1|1];
       Min[rt]=a[Min[rt<<1]]<a[Min[rt<<1|1]]?Min[rt<<1]:Min[rt<<1|1];
}

ll  query_max(ll  rt,ll  l,ll  r,ll  ql,ll  qr)
{
      if(ql<=l&&qr>=r)
      {
            return Max[rt];
      }
      ll  xx1=ql,xx2;
      ll  m=(l+r)>>1;
      if(m>=ql)
         xx1=query_max(rt<<1,l,m,ql,qr);
      if(m<qr)
      {
         xx2=query_max(rt<<1|1,m+1,r,ql,qr);
         if(a[xx1]<a[xx2])
              xx1=xx2;
      }
      return xx1;
}

ll  query_min(ll  rt,ll  l,ll  r,ll  ql,ll  qr)
{
        if(ql<=l&&qr>=r)
        {
             return Min[rt];
        }
        ll  m=(l+r)>>1;
        ll  xx1=ql,xx2;
       if(m>=ql)
           xx1=query_min(rt<<1,l,m,ql,qr);
       if(m<qr)
       {
           xx2=query_min(rt<<1|1,m+1,r,ql,qr);
           if(a[xx1]>a[xx2])
                xx1=xx2;
        }
       return xx1;
}

int  main()
{
      while(scanf("%lld%lld",&n,&k)!=EOF)
      {
             for(int ii=1;ii<=n;ii++)
                  scanf("%lld",&a[ii]);
            Build(1,1,n);
            ll  l=1,r=1;
            ll  Ma=1,Mi=1;
            ll  ans=1;
            while(1)
            {
                    r++;
                    if(r>n)
                        break;
                    if(a[r]>=a[Mi]&&a[r]<=a[Ma])
                    {
                        if(ans<r-l+1)
                            ans=r-l+1;
                    }
                    else if(a[r]<a[Mi])
                    {
                        Mi=r;
                        while((a[Ma]-a[Mi])>k)
                        {
                            l=Ma+1;
                            Ma=query_max(1,1,n,l,r);
                        }
                        if(ans<r-l+1)
                           ans=r-l+1;
                    }
                    else
                    {
                        Ma=r;
                        while((a[Ma]-a[Mi])>k)
                        {
                            l=Mi+1;
                            Mi=query_min(1,1,n,l,r);
                        }
                        if(ans<r-l+1)
                           ans=r-l+1;
                    }
           }
         printf("%lld\n",ans);
      }
   return 0;
}
时间: 2024-10-07 20:04:40

CUSOJ1170--A Simple Problem的相关文章

NYOJ 707 A Simple Problem(结构体排序) 睡前一水~~

链接:click here 题意: A Simple Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 You know, just as the title imply, this is a simple problem. In a contest, given the team-id, solved, penalty of all the teams, tell me the champion.If the numbers of solved pr

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe

【multiset】hdu 5349 MZL&#39;s simple problem

[multiset]hdu 5349 MZL's simple problem 题目链接:hdu 5349 MZL's simple problem 题目大意 n次操作,插入元素.删除最小元素.查询最大元素并输出. C++STL的multiset的使用 set--多元集合(元素不可重复),multiset--可重复元素的多元集合 多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象.(具体用法请参照set容器) set和multiset内部是以平衡二叉树实现的: 从内部数据结

Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 73239   Accepted: 22607 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of