[CF6E]Exposition 题解

[CF6E]Exposition 题解

题目地址:Codeforces:Problem – 6E – Codeforces、洛谷:CF6E Exposition – 洛谷 | 计算机科学教育新生态

题目描述

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.

题意简述

给定一个长度为$n$的数列$h_i$,求该序列中最长的极差不超过$k$的子段(连续一段)长度与数量。你需要输出每一个子段的起止下标。

输入输出格式

输入格式:
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.

输入输出样例

输入样例#1:

3 3
14 12 10

输出样例#1:

2 2
1 2
2 3

输入样例#2:

2 0
10 10

输出样例#2:

2 1
1 2

输入样例#3:

4 5
8 19 10 13

输出样例#3:

2 1
3 4

题解

看到这个题的第一反应就是二分答案,于是转换为判断每个长度为二分出来的值$m$的子段极差,由于极差等于最大值减最小值,维护最大值和最小值是一个滑动窗口问题,可以使用单调队列快速解决。

需要注意的是,本题需要计数并输出方案,因此可以在check函数内维护一个vector装符合要求的子段端点。

总复杂度$O(n \log n)$。

代码

// Code by KSkun, 2019/8
#include <cstdio>
#include <cctype>
#include <cstring>

#include <algorithm>
#include <queue>
#include <vector>

typedef long long LL;

inline char fgc() {
	static char buf[100000], * p1 = buf, * p2 = buf;
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
		? EOF : *p1++;
}

inline LL readint() {
	LL res = 0, neg = 1; char c = fgc();
	for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
	for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
	return res * neg;
}

inline char readsingle() {
	char c;
	while(!isgraph(c = fgc())) {}
	return c;
}

const int MAXN = 100005;

int n, k, h[MAXN];
std::vector<int> vec;

inline int check(int mid) {
	std::deque<int> mx, mn; vec.clear();
	int cnt = 0;
	for(int i = 1; i < mid; i++) {
		while(!mx.empty() && h[mx.back()] <= h[i]) mx.pop_back();
		while(!mn.empty() && h[mn.back()] >= h[i]) mn.pop_back();
		mx.push_back(i); mn.push_back(i);
	}
	for(int i = mid; i <= n; i++) {
		while(!mx.empty() && h[mx.back()] <= h[i]) mx.pop_back();
		while(!mn.empty() && h[mn.back()] >= h[i]) mn.pop_back();
		mx.push_back(i); mn.push_back(i);
		while(!mx.empty() && mx.front() < i - mid + 1) mx.pop_front();
		while(!mn.empty() && mn.front() < i - mid + 1) mn.pop_front();
		if(h[mx.front()] - h[mn.front()] <= k) {
			cnt++; vec.push_back(i);
		}
	}
	return cnt;
}

int main() {
	n = readint(); k = readint();
	for(int i = 1; i <= n; i++) {
		h[i] = readint();
	}
	h[n + 1] = 1e9;
	int l = 1, r = n + 1, mid;
	while(r - l > 1) {
		mid = (l + r) >> 1;
		if(check(mid)) l = mid;
		else r = mid;
	}
	printf("%d %d\n", l, check(l));
	for(auto i : vec) {
		printf("%d %d\n", i - l + 1, i);
	}
	return 0;
}


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据