您的位置:首页 > 其它

【SPOJ-ARRAYSUB】subarrays【单调队列】

2016-02-21 18:32 316 查看
题意:

给出n个数,问连续k个最大值。

裸的单调队列。

一发AC。

(把k给在数后面大概是为了防止读入时就处理数据吧...)

#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 1000005;

int n, k, num[maxn];

struct _data {
	int w, pos;
} q[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) num[i] = iread();
	k = iread(); k = min(k, n);

	int h = 1, t = 0;
	for(int i = 1; i < k; i++) {
		for(; h <= t && q[t].w <= num[i]; t--);
		q[++t] = (_data) {num[i], i};
	}

	for(int i = k; i <= n; i++) {
		for(; h <= t && q[h].pos <= i - k; h++);
		for(; h <= t && q[t].w <= num[i]; t--);
		q[++t] = (_data) {num[i], i};
		printf("%d ", q[h].w);
	}

	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: