首先就是对于超时的做法进行展示,这个思路是最原始的思路,也就是用单调队列的板子,用了两次,然后求差值,差值满足条件我们就直接返回这个长度,然后和后面的循环的长度进行相比。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include <iomanip>
#include<sstream>
#include<numeric>
#include<map>
#include<limits.h>
#include<unordered_map>
#include<set>
#define int long long
#define MAX 300100
#define inf 0x3f3f3f3f
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef pair<int, int> PII;
int n, m, k;
int counts=inf;
int dx[] = { 0,1,0,-1};
int dy[] = { 1,0,-1,0 };
int q[MAX];
int arr[MAX];
int b1[MAX];
int b2[MAX];
int maxs,mins=inf;
void get_max(int a[], int b[], int n, int qujian) {int front = 0;int rear = -1;for (int i = 0; i < n; i++) {if (front <= rear && qujian + q[front] <= i)front++;while (front <= rear && a[q[rear]] <= a[i])rear--;q[++rear] = i;if (i >= qujian - 1)b[i] = a[q[front]];}}
void get_min(int a[], int b[], int n, int k) {int front = 0;int rear = -1;for (int i = 0; i < n; i++) {if (front <= rear && q[front] + k <= i) {front++;}while (front <= rear && a[q[rear]] >= a[i])rear--;q[++rear] = i;b[i] = a[q[front]];}
}
signed main() {ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);cin >> k >> n;for (int i = 0; i < n; i++) {cin >> arr[i];}int res = 0;for (int i = n; i >= 1; i--) {memset(q, 0, sizeof q);memset(b1, 0, sizeof b1);memset(b2, 0, sizeof b2);get_max(arr, b1, n, i);memset(q, 0, sizeof q);get_min(arr, b2, n, i);for (int j = i - 1; j < n; j++) {if (b1[j] - b2[j] <= k) {res = max(res, i);break;}}}cout << res;return 0;
}
接下来进行优化时,其实就是对于枚举的时候用了二分而已,也就是复杂度从On2变成了Onlogn
上代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include <iomanip>
#include<sstream>
#include<numeric>
#include<map>
#include<limits.h>
#include<unordered_map>
#include<set>
#define int long long
#define MAX 2000000
#define inf 0x3f3f3f3f
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef pair<int, int> PII;
int n, m, k;
int counts=inf;
int dx[] = { 0,1,0,-1};
int dy[] = { 1,0,-1,0 };
int q[MAX];
int arr[MAX];
int b[MAX];
int extra[MAX];
int maxs;
bool get_max(int a[], int b[],int b2[], int n, int qujian) {int front = 0;int rear = -1;for (int i = 0; i < n; i++) {if (front <= rear && qujian + q[front] <= i)front++;while (front <= rear && a[q[rear]] <= a[i])rear--;q[++rear] = i;if (i >= qujian - 1)b[i] = a[q[front]];}memset(q, 0, sizeof q);front = 0;rear = -1;for (int i = 0; i < n; i++) {if (front <= rear && qujian + q[front] <= i)front++;while (front <= rear && a[q[rear]] >= a[i])rear--;q[++rear] = i;if (i >= qujian - 1)b2[i] = a[q[front]];}for (int i = qujian - 1; i < n; i++) {if (b[i] - b2[i] <= k) {return true;}}return false;
}signed main() {ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);cin >> k >> n;for (int i = 0; i < n; i++) {cin >> arr[i];}int l = 1;int r = n;while (l < r) {memset(b, 0, sizeof b);memset(extra, 0, sizeof extra);int mid = (l + r + 1) / 2;if (get_max(arr, b, extra, n, mid))l = mid;elser = mid - 1;}cout << l;return 0;
}