codeforce 6E 线段树+枚举

来源:互联网 发布:淘宝微淘如何发买家秀 编辑:程序博客网 时间:2024/05/01 00:33

http://codeforces.com/problemset/problem/6/E

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.

Sample test(s)
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
题目大意:给定一个数组,求出这个数组里面的最长的连续子序列,满足该序列中的最大值和最小值相差不超过k,并输出满足其开始和结尾数字的位置,若有多个分别输出。

简单思路:利用线段树求出每个区间段的数的最大最小值,再依次枚举每个区间即可。

AC代码:

#include <stdio.h>#include <iostream>using namespace std;const int mm=111111;int ma[mm<<2],mi[mm<<2],ql[mm],qr[mm];int i,j,k,n,ans,t;void build(int l,int r,int rt){    if(l==r)    {        scanf("%d",&ma[rt]);        mi[rt]=ma[rt];        return;    }    int m=(l+r)>>1;//相当于除二;    build(l,m,rt<<1);    build(m+1,r,rt<<1|1);    ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);    mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);}int queryma(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r) return ma[rt];    int m=(l+r)>>1,ret=0;    if(L<=m) ret=max(ret,queryma(L,R,l,m,rt<<1));    if(R>m)ret=max(ret,queryma(L,R,m+1,r,rt<<1|1));    return ret;}int querymi(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)        return mi[rt];    int m=(l+r)>>1,ret=2e9;    if(L<=m) ret=min(ret,querymi(L,R,l,m,rt<<1));    if(R>m) ret=min(ret,querymi(L,R,m+1,r,rt<<1|1));    return ret;}int main(){    while(~scanf("%d%d",&n,&k))    {        build(1,n,1);        ans=t=0;        for(i=j=1;j<=n;i++)        {            if(j<i)                j=i;            while(j<=n&&(queryma(i,j,1,n,1)-querymi(i,j,1,n,1)<=k))                ++j;            if(j-i>ans)                ans=j-i,ql[0]=i,qr[0]=j-1,t=1;            else if(j-i==ans)                ql[t]=i,qr[t++]=j-1;        }        printf("%d %d\n",ans,t);        for(i=0;i<t;i++)            printf("%d %d\n",ql[i],qr[i]);    }    return 0;}

再写一个wa的代码,我利用尺取法做的

2290734YranEWrong answer on test 33  GNU C++ 4.6望高人看到指点

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int a[100005];struct note{    int sum;    int x,y;}edge[100005];bool cmp(note a,note b){    if(a.sum==b.sum)        return a.x<b.x;    return a.sum>b.sum;}int main(){    int n,k;    while(~scanf("%d%d",&n,&k))    {        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        if(n==1)        {            printf("1 1\n1 1\n");            continue;        }        int maxn=a[1],minn=a[1],xmaxn=1,xminn=1;        int x=1,y=1;        int flag=0;        int ip=0;        for(int i=2;i<=n;i++)        {            if(a[i]>maxn)            {                xmaxn=i;                maxn=a[i];               // printf("&&\n");            }            if(a[i]<minn)            {                xminn=i;                minn=a[i];             //   printf("**\n");            }            if(maxn-minn>k)            {                edge[ip].sum=y-x+1;                edge[ip].x=x;                edge[ip++].y=y;                if(a[i]==maxn)                {                    for(int j=xminn;j<=xmaxn;j++)                    {                        if(maxn-a[j]<=k)                        {                            xminn=j;                            minn=a[j];                            x=j;                            break;                        }                    }                }                else                {                    for(int j=xmaxn;j<=xminn;j++)                    {                        if(a[j]-minn<=k)                        {                            //printf("**,%d\n",a[j]);                            xmaxn=j;                            maxn=a[j];                            x=j;                            break;                        }                    }                }            }            if(i==n)            {                edge[ip].sum=y+1-x+1;                edge[ip].x=x;                edge[ip++].y=y+1;            }            y=i;        }        int sum1=0;        /*for(int i=0;i<ip;i++)        {            printf("(%d,%d,%d)\n",edge[i].sum,edge[i].x,edge[i].y);        }*/        sort(edge,edge+ip,cmp);        int m=edge[0].sum;        for(int i=0;i<ip;i++)        {            if(edge[i].sum>=m)                sum1++;            else                break;        }        printf("%d %d\n",m,sum1);        for(int i=0;i<sum1;i++)        {            printf("%d %d\n",edge[i].x,edge[i].y);        }    }    return 0;}



0 0
原创粉丝点击