CodeForces Exposition && 单调队列

来源:互联网 发布:兴趣图谱源码 编辑:程序博客网 时间:2024/06/04 00:12

E. Exposition
time limit per test
1.5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.

The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.

Input

The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.

Output

In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.

In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.

Examples
input
3 314 12 10
output
2 21 22 3
input
2 010 10
output
2 11 2
input
4 58 19 10 13
output
2 13 4

传送门:http://codeforces.com/problemset/problem/6/E


题意:给一个n个元素的序列,从中挑出最长的子序列,要求子序列中元素差的最大值不超过k。问有几个最长子序列,子序列长度,以及这几个子序列的起始、终止位置。


思路:此题先想到了用线段树查询区间最大最小值(RMQ),后想到单调队列,两种方法试了一遍,RMQ迷之WA


先贴上单调队列原理:

单调队列我们从最简单的问题开始:给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k.要求:      f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0,1,...,N-1问题的另一种描述就是用一个长度为k的窗在整数数列上移动,求窗里面所包含的数的最大值。解法一:很直观的一种解法,那就是从数列的开头,将窗放上去,然后找到这最开始的k个数的最大值,然后窗最后移一个单元,继续找到k个数中的最大值。这种方法每求一个f(i),都要进行k-1次的比较,复杂度为O(N*k)。那么有没有更快一点的算法呢?解法二:我们知道,上一种算法有一个地方是重复比较了,就是在找当前的f(i)的时候,i的前面k-1个数其它在算f(i-1)的时候我们就比较过了。那么我们能不能保存上一次的结果呢?当然主要是i的前k-1个数中的最大值了。答案是可以,这就要用到单调递减队列。单调递减队列是这么一个队列,它的头元素一直是队列当中的最大值,而且队列中的值是按照递减的顺序排列的。我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。1.首先看插入元素:为了保证队列的递减性,我们在插入元素v的时候,要将队尾的元素和v比较,如果队尾的元素不大于v,则删除队尾的元素,然后继续将新的队尾的元素与v比较,直到队尾的元素大于v,这个时候我们才将v插入到队尾。2.队尾的删除刚刚已经说了,那么队首的元素什么时候删除呢?由于我们只需要保存i的前k-1个元素中的最大值,所以当队首的元素的索引或下标小于 i-k+1的时候,就说明队首的元素对于求f(i)已经没有意义了,因为它已经不在窗里面了。所以当index[队首元素]<i-k+1时,将队首 元素删除。从上面的介绍当中,我们知道,单调队列与队列唯一的不同就在于它不仅要保存元素的值,而且要保存元素的索引(当然在实际应用中我们可以只需要保存索引,而通过索引间接找到当前索引的值)。为了让读者更明白一点,我举个简单的例子。假设数列为:8,7,12,5,16,9,17,2,4,6.N=10,k=3.那么我们构造一个长度为3的单调递减队列:首先,那8和它的索引0放入队列中,我们用(8,0)表示,每一步插入元素时队列中的元素如下:0:插入8,队列为:(8,0)1:插入7,队列为:(8,0),(7,1)2:插入12,队列为:(12,2)3:插入5,队列为:(12,2),(5,3)4:插入16,队列为:(16,4)5:插入9,队列为:(16,4),(9,5)。。。。依此类推那么f(i)就是第i步时队列当中的首元素:8,8,12,12,16,16,。。。



AC代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<deque>#include<vector>using namespace std;const int MAXN = 1e5 + 10;int book[MAXN];vector<pair<int, int> > v;deque<int> high;deque<int> low;int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        for(int i=1;i<=n;++i){            scanf("%d",&book[i]);        }        v.clear();        high.clear();        low.clear();        int l,r;        int ans=0,cnt=0;        for(l=r=1;r<=n;++r){            while(!high.empty()&& high.back() < book[r]) high.pop_back();            high.push_back(book[r]);            while(!low.empty()&& low.back() > book[r]) low.pop_back();            low.push_back(book[r]);            while(!high.empty()&&!low.empty()&& high.front()-low.front()>m){                if(v.empty()||(r-l)>ans){                    v.clear();                    cnt=1;                    ans=r-l; //r为越界的r,不用+1;                    v.push_back(make_pair(l,r-1));                }                else if(!v.empty()&&(r-l)==ans){                    cnt++;                    v.push_back(make_pair(l,r-1));                }                //最大或最小值不属于区间,则pop出队列                if(high.front()==book[l]) high.pop_front();                if(low.front()==book[l]) low.pop_front();                l++;            }        }        //一定要走到最后        while(l<=n){             if(v.empty()||(r-l)>ans){                    v.clear();                    cnt=1;                    ans=r-l; //r为越界的r,不用+1;                    v.push_back(make_pair(l,r-1));                }                else if(!v.empty()&&(r-l)==ans){                    cnt++;                    v.push_back(make_pair(l,r-1));                }                //最大或最小值不属于区间,则pop出队列                if(high.front()==book[l]) high.pop_front();                if(low.front()==book[l]) low.pop_front();                l++;        }        printf("%d %d\n",ans,cnt);        for(int i=0;i<v.size();++i){            printf("%d %d\n",v[i].first,v[i].second);        }    }}