记录编号 575009 评测结果 AAAAAAAAAA
题目名称 双倍腹肌量 最终得分 100
用户昵称 Gravatarnick 是否通过 通过
代码语言 C++ 运行时间 0.248 s
提交时间 2022-08-31 20:59:21 内存使用 4.41 MiB
显示代码纯文本
  1. #include<bits/stdc++.h>
  2. #define ri register int
  3. #define N 100005
  4. #define read(a) a=read1()
  5. using namespace std;
  6. char nc(){
  7. static char buf[100000],*p1=buf,*p2=buf;
  8. return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  9. }
  10. int read1( ){
  11. static char c=nc();ri x,f=1;
  12. for(;c>'9'||c<'0';c=nc()) if(c=='-') f=-1;
  13. for(x=0;c<='9'&&c>='0';c=nc()) x=(x<<3)+(x<<1)+c-48;
  14. return x*f;
  15. }
  16. int n,d,h1,h2,t1,t2,ans=1e9;
  17. int q1[N],q2[N];
  18. struct node{int x,y;}a[N];
  19. bool cmp(node a,node b){return a.x < b.x;}
  20. int main()
  21. {
  22. freopen("double_muscle.in","r",stdin);
  23. freopen("double_muscle.out","w",stdout);
  24. ri l=1,i,r;h1=h2=1;
  25. read(n);read(d);
  26. for(i=1;i<=n;++i) read(a[i].x),read(a[i].y);
  27. sort(a+1,a+n+1,cmp);
  28. for(l=1,r=0;l<=n;++l)
  29. {
  30. while(h1<=t1&&q1[h1]<l) ++h1; // 维护滑动窗口
  31. while(h2<=t2&&q2[h2]<l) ++h2;
  32. while(a[q1[h1]].y-a[q2[h2]].y<d&&r<n)
  33. {
  34. ++r;
  35. while(a[q1[t1]].y<a[r].y&&h1<=t1)--t1;q1[++t1]=r; //维护单调队列
  36. while(a[q2[t2]].y>a[r].y&&h2<=t2)--t2;q2[++t2]=r;
  37. }
  38. if(a[q1[h1]].y-a[q2[h2]].y>=d)ans=min(ans,a[r].x-a[l].x); //更新答案
  39. }
  40. printf("%d",ans>=1e9?-1:ans);
  41. return 0;
  42. }