Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
34038 LYLAKIOI 【S】T1 C++ 通过 100 652 MS 4168 KB 1799 2024-11-03 13:20:59

Tests(10/10):


#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back typedef long long ll; using namespace std; const int maxn=5e5+10; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n,k,a[maxn]; int q1[maxn],q2[maxn]; int t[maxn]; void slv(){ n=read(),k=read(); up(i,1,n)a[i]=read(); int l1=1,r1=0,l2=1,r2=0; int lim=2e9; up(i,1,n){ while(l1<=r1&&i-q1[l1]>n-k)l1++; while(l2<=r2&&i-q2[l2]>n-k)l2++; while(l1<=r1&&a[q1[r1]]<=a[i])--r1;q1[++r1]=i; while(l2<=r2&&a[q2[r2]]>=a[i])--r2;q2[++r2]=i; if(i>=n-k+1)lim=min(lim,a[q1[l1]]-a[q2[l2]]); } printf("%d ",lim); vector<int>vec; up(i,1,n)t[i]=0; l1=1,r1=0,l2=1,r2=0; int pos=0; up(i,1,n){ if(pos<i){ l1=1,r1=1,l2=1,r2=1; q1[1]=q2[1]=i;pos=i; } while(pos<=n&&a[q1[l1]]-a[q2[l2]]<lim){ ++pos; if(pos>n)break; while(l1<=r1&&a[q1[r1]]<=a[pos])--r1;q1[++r1]=pos; while(l2<=r2&&a[q2[r2]]>=a[pos])--r2;q2[++r2]=pos; } if(pos<=n)t[pos-i+1]++,t[n-i+2]--;else break; while(l1<=r1&&q1[l1]<=i)l1++; while(l2<=r2&&q2[l2]<=i)l2++; } up(i,1,n)t[i]+=t[i-1]; up(i,1,n)if(t[i]>=k){printf("%d\n",i);break;} } int main(){ //freopen("choose.in","r",stdin); //freopen("choose.out","w",stdout); int t=read();while(t--)slv(); fclose(stdin); fclose(stdout); return 0; }


测评信息: