跳到正文

个人记录

fyhucvgbu

Ch_Elowen约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年11月1日
收录
2026年11月1日
更新
2026年11月1日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std ;
int n , d ;
int a[3000005] ;
int q[3000005] , qw[3000005] ;
int read()
{
    int x = 0 , f = 1 ;
    char c = getchar() ;
    while(c < '0' || c > '9')
    {
        if(c == '-') f = -1 ;
        c = getchar() ;
    }
    while(c >= '0' && c <= '9')
    {
        x = x * 10 + (c - '0') ;
        c = getchar() ;
    }
    return x * f ;
}
bool check(int x)
{
    int sizq = 0 , sizw = 0 , sq = 1 , sw = 1 ;
    for(int i = 1 ; i <= n ; i++)
    {
        if(sizq >= sq && i - q[sq] >= x) sq++ ;
        while(sizq >= sq && a[i] >= a[q[sizq]]) sizq-- ;
        q[++sizq] = i ;
        
        if(sizw >= sw && i - qw[sw] >= x) sw++ ;
        while(sizw >= sw && a[i] <= a[qw[sizw]]) sizw-- ;
        qw[++sizw] = i ;

        if(i >= x && a[q[sq]] - a[qw[sw]] <= d) return true ;
    }
    return false ;
}
int main()
{
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    cout.tie(0) ;
	d = read() , n = read() ;
    for(int i = 1 ; i <= n ; i++) a[i] = read() ;
    int l = 1 , r = n ;
    while(l < r)
    {
        int pos = (l + r + 1) / 2 ;
        if(check(pos) == true) l = pos ;
        else r = pos - 1 ;
    }
    cout << r ;
return 0 ;
}
原文