CodeForces - 6E Exposition【ST】

【题目描述】
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.

【输入】
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.

【输出】
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.

【样例输入】
3 3
14 12 10

【样例输出】
2 2
1 2
2 3

【样例输入】
2 0
10 10

【样例输出】
2 1
1 2

【样例输入】
4 5
8 19 10 13

【样例输出】
2 1
3 4

题目链接:https://codeforces.com/contest/6/problem/E

代码如下:

#include <bits/stdc++.h>
using namespace std;
static const int MAXN=100000;
int n,k;
int h[MAXN+10],f1[MAXN+10][20],f2[MAXN+10][20],ans[MAXN+10];
void ST_pre()
{
    for(int i=1;i<=n;i++)
        f1[i][0]=f2[i][0]=h[i];
    int t=log(n)/log(2)+1;
    for(int j=1;j<t;j++)
        for(int i=1;i<=n-(1<<j)+1;i++)
        {
            f1[i][j]=max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
            f2[i][j]=min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
        }
}
int ST_query(int l,int r)
{
    int k=log(r-l+1)/log(2);
    return max(f1[l][k],f1[r-(1<<k)+1][k])-min(f2[l][k],f2[r-(1<<k)+1][k]);
}
int main()
{
    cin>>n>>k;
    int beg=1,l=0,cnt=0;
    for(int i=1;i<=n;i++)
        cin>>h[i];
    ST_pre();
    for(int i=1;i<=n;i++)
    {
        while(ST_query(beg,i)>k) beg++;
        if(i-beg+1==l)
            ans[cnt++]=beg;
        else if(i-beg+1>l)
        {
            l=i-beg+1;
            cnt=0;
            ans[cnt++]=beg;
        }
    }
    cout<<l<<" "<<cnt<<endl;
    for(int i=0;i<cnt;i++)
        cout<<ans[i]<<" "<<ans[i]+l-1<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值