3 条题解
-
4
#include<bits/stdc++.h> #define debug(...) fprintf(stderr,VA_ARGS) #define DEBUG printf("Passing [%s] in LINE %d\n",FUNCTION,LINE) #define Debug debug("Passing [%s] in LINE %d\n",FUNCTION,LINE) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; const double eps=1e-8; const double pi=acos(-1.0); typedef long long ll; typedef pair<int,int> pii; template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;} template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return aa;} template<class T>T mmin(T a,T b){return a<b?a:b;} template<class T>T mmax(T a,T b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<int a>int cmp_a(int x,int y){return a[x]<a[y];} #define min mmin #define max mmax #define abs aabs int read(){ int s=0,base=1; char c; while(!isdigit(c=getchar()))if(c=='-')base=-base; while(isdigit(c)){s=s10+(c^48);c=getchar();} return sbase; } bool cx[1048575]; int mn[1048575],mx[1048575]; template<const function<int(int,int)> &cmp> struct MQ{ int s[1048575],*l,*r; MQ(){l=s+1;r=s;} void add(int x){ while(l<=r && cmp(x,*r))--r; *(++r)=x; } void del(int x){ if(*lx)++l; } int top(){return *l;} }; int increase(int a,int b){return a<b;} int decrease(int a,int b){return a>b;} function<int(int,int)> Inc=increase; function<int(int,int)> Dec=decrease; MQ<Inc> inc; MQ<Dec> dec_; int main(){ #ifdef cnyali_lk freopen("2698.in","r",stdin); freopen("2698.out","w",stdout); #endif int n,d,x,y,l,r,xmn=0x3f3f3f3f; n=read();d=read(); for(int i=0;i<=1000000;++i)mn[i]=0x3f3f3f3f,mx[i]=-0x3f3f3f3f; for(int i=1;i<=n;++i){ x=read();y=read(); chkmax(mx[x],y); chkmin(mn[x],y); cx[x]=1; chkmin(xmn,x); } l=xmn,r=xmn; inc.add(mn[l]); dec_.add(mx[l]); int ans=0x3f3f3f3f; n=1000000; while(r<=n){ while(r<=n && dec_.top()-inc.top()<d){++r;if(cx[r]){inc.add(mn[r]);dec_.add(mx[r]);}} while(l<r && dec_.top()-inc.top()>=d){if(cx[l]){inc.del(mn[l]);dec_.del(mx[l]);}++l;} if(r<=n) chkmin(ans,r-l+1); } if(ans0x3f3f3f3f)printf("-1\n"); else printf("%d\n",ans); return 0; }
-
3
#include<bits/stdc++.h> using namespace std; void read(int &x){//快读 int f=1;x=0; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();} x*=f; } #define N 100010 int n,d,ans; struct node{ int x,y; }a[N]; bool cmp(node aa,node bb){ return aa.x<bb.x; } int p1[N],p2[N];//我写单调队列一般用 q[] 存具体值,用 p[]存位置 int head1,head2,tail1,tail2;//队列 1存最大值,队列 2存最小值 int main(){ read(n),read(d);//如题所示 for(int i=1;i<=n;i++){ read(a[i].x),read(a[i].y);//结构体,因为要排序 } sort(a+1,a+1+n,cmp);//排序 ans=0x3f3f3f3f; head1=head2=1; for(int le=0,r=0;le<=n;le++){//最外层循环拉左端点 while(head1<=tail1&&p1[head1]<le) head1++;//如果队首比左端点小,那么右移 while(head2<=tail2&&p2[head2]<le) head2++;//因为排序过了,编号小x一定小 while(a[p1[head1]].y-a[p2[head2]].y< d && r<n){ r++; while(head1<=tail1&&a[p1[tail1]].y<a[r].y) tail1--; p1[++tail1]=r; while(head2<=tail2&&a[p2[tail2]].y>a[r].y) tail2--; p2[++tail2]=r;//更新两个单调队列 } if(r!=n) ans=min(ans,a[r].x-a[le].x);//满足条件 } if(ans>=0x3f3f3f3f){//判无解 printf("-1");return 0; } else{ printf("%d",ans); } }
- 1
信息
- ID
- 1848
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 66
- 已通过
- 0
- 上传者