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.
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
简单思路:利用线段树求出每个区间段的数的最大最小值,再依次枚举每个区间即可。
AC代码:
#include
#include
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(jans)
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
2290734 | Yran | E | Wrong answer on test 33 | GNU C++ 4.6 |
#include
#include
#include
#include
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.xb.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]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=m)
sum1++;
else
break;
}
printf("%d %d\n",m,sum1);
for(int i=0;i