cf—E. Exposition单调队列

来源:互联网 发布:淘宝论文查重可靠吗 编辑:程序博客网 时间:2024/06/10 14:33

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 chrono
logical 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

这道题表示做不出来呐,虽然看上去不难。

所以就百度了,尽管是百度的,但至少弄懂了,只要做到下次遇到会做就可以了。

码基本是抄别人的,然后思路是看码得出来的,看了很久才看懂的,而且涉及单调队列的问题,还用上了双端队列,表示一脸蒙逼。

题意:给你一个数列,n个元素,要你找出连续子序列中最大和最小的差小于k的最长子序列。输出这种数列的个数,再输出连续子序列的区间下标。

思路:遍历i,将h[ i ]放入单调递增和单调递减队列中,然后如果递增队列最大值减去最小值大于k,那么判断答案是否最大,是的话放入容器中,然后 j 递增,递增前判断是否需要改变递增队列,是的话就改变。然后j++,一直到循环结束。

然后因为j还没到n,于是移动j,并判断是否是最优答案,是的话就更改答案容器,最后就是输出答案了。

然后是代码:

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <sstream>#include <fstream>#include <set>#include <map>#define INF 1e9using namespace std;//typedef long long ll;typedef pair<int,int> P;int n,h[100050],k;vector<P> G;deque<int> high;deque<int> low;int main(void){    scanf("%d %d",&n,&k);    for (int i = 1;i <= n;i++)    {        scanf("%d",&h[i]);    }    G.clear();    high.clear();    low.clear();    int i,j;    int ans = 0,cnt = 0;    for (i = j = 1;i <= n;i++)    {        while (!high.empty()&&high.back()<h[i]) high.pop_back();        high.push_back(h[i]);        while (!low.empty()&&low.back()>h[i]) low.pop_back();        low.push_back(h[i]);        while (!high.empty()&&!low.empty()&&high.front()-low.front()>k)        {            if(G.empty()||i-j>ans)            {                G.clear();                cnt = 1;                ans = i-j;                G.push_back(P(j,i-1));            }            else if(!G.empty()&&i-j==ans)            {                cnt++;                G.push_back(P(j,i-1));            }            if(high.front() == h[j]) high.pop_front();            if(low.front() == h[j]) low.pop_front();            j++;        }    }    while (j <= n)    {        if(G.empty()||i-j>ans)        {            G.clear();            cnt = 1;            ans = i-j;            G.push_back(P(j,i-1));        }        else if(!G.empty()&&i-j == ans)        {            cnt++;            G.push_back(P(j,i-1));        }        if(high.front()==h[j]) high.pop_front();        if(low.front()==h[j]) low.pop_front();        j++;    }    cout<<ans<<" "<<cnt<<endl;    for (int i = 0;i < G.size();i++)    {        cout<<G[i].first<<" "<<G[i].second<<endl;    }    return 0;}
多刷题,而且要一套一套刷,不然一直刷那些自己会的根本没什么用!!!


0 0
原创粉丝点击