codeforces 6E. Exposition(RMQ + 二分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjw6463/article/details/51493183
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 3
14 12 10
Output
2 2
1 2
2 3
Input
2 0
10 10
Output
2 1
1 2
Input
4 5
8 19 10 13
Output
2 1
3 4


题意:给你n个数和k,让你求区间的最长长度,并且区间里的最大值与最小值的差小于等于k, 并把这些区间的位置表示出来


思路:直接用rmq求出区间的最大值和最小值, 然后二分长度求出最大满足条件的区间长度


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int mod = 1000000007;
typedef long long LL;
#pragma comment(linker, "/STACK:102400000,102400000")
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
int mmax[100005][20], mmin[100005][20], a[100005];
int n, k;
struct node
{
	int x, y;
};
vector<node>vc;
void rmq()
{
	for (int i = 1; i <= n; ++i)
	{
		mmax[i][0] = a[i];
		mmin[i][0] = a[i];
	}
	for (int j = 1; (1 << j) <= n; ++j)
	{
		for (int i = 1; i + (1 << j) - 1 <= n; ++i)
		{
			mmax[i][j] = max(mmax[i][j - 1], mmax[i + (1 << (j - 1))][j - 1]);
			mmin[i][j] = min(mmin[i][j - 1], mmin[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int cal(int len)
{
	int p = (int)(log(len * 1.0) / log(2.0));
	int res = 0;
	for (int i = 1; i + len - 1<= n; ++i)
	{
		if (max(mmax[i][p], mmax[i + len - (1 << p)][p]) - min(mmin[i][p], mmin[i + len - (1 << p)][p]) <= k)
		{
			res++;
			//return 1;
		}
	}
	return res;
}
int main()
{
	int i, j;
	while (cin >> n >> k)
	{
		//vc.clear();
		for (i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
		}
		rmq();
		int ans = 1;
		int l = 1, r = n;
		int res = 0;
		while (l <= r)
		{
			int mid = (l + r) / 2;
			if (cal(mid))
			{
				ans = max(ans, mid);
				res = cal(mid);
				l = mid + 1;
			}
			else
				r = mid - 1;
		}
		int len = ans;
		cout << ans << " "<<res<<endl;
		int p = (int)(log(len * 1.0) / log(2.0));
		for (i = 1; i  + len - 1<= n; ++i)
		{
			if (max(mmax[i][p], mmax[i + len - (1 << p)][p]) - min(mmin[i][p], mmin[i + len - (1 << p)][p]) <= k)
			{
				cout << i << " " << i + len - 1 << endl;
			}
		}
	}
}





猜你喜欢

转载自blog.csdn.net/zjw6463/article/details/51493183