30分求助
查看原帖
30分求助
816549
52wyd楼主2023/5/11 10:35

##单调队列做法,只有30分

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <deque>

using namespace std;

const int N = 3e6 + 10;

int n, k;
int a[N];
deque <int> q;
// front 到 back 所存储的下标对应的元素单调递增
int res;

int main() {
	
	cin >> k >> n;
	
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	
	for (int i = 1; i <= n; i ++) {
		while (!q.empty() && a[q.back()] > a[i]) {
			q.pop_back();
		}
		q.push_back(i);
		if (q.size() > 1 && a[q.back()] - a[q.front()] <= k) {
			res = max(res, q.back() - q.front() + 1);
		}
		while (q.size() > 1 && a[q.back()] - a[q.front()] > k) {
			q.pop_front();
		}
	}
	
	cout << res << endl;
	return 0;
}



2023/5/11 10:35
加载中...