P2564-[USACO12MAR]花盆Flowerpot

  1 #include<bits/stdc++.h>
  2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
  3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
  4 #define INF 0x3f3f3f3f
  5 #define MOD 100000000
  6 #define maxn 1000003
  7 #define pb push_back
  8 #define debug() printf("Miku Check OK!\n")
  9 typedef long long ll;
 10 
 11 using namespace std;
 12 
 13 inline ll read()
 14 {
 15     ll ans = 0;
 16     char ch = getchar(), last = ' ';
 17     while(!isdigit(ch)) last = ch, ch = getchar();
 18     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
 19     if(last == '-') ans = -ans;
 20     return ans;
 21 }
 22 inline void write(ll x)
 23 {
 24     if(x < 0) x = -x, putchar('-');
 25     if(x >= 10) write(x / 10);
 26     putchar(x % 10 + '0');
 27 }
 28 struct Point
 29 {
 30     int x;
 31     int y;
 32     bool operator < (Point b)
 33     {
 34         return x < b.x;
 35     }
 36 };
 37 struct Num
 38 {
 39     int x;
 40     int y;
 41 };
 42 
 43 int n,d;
 44 Point in[maxn];
 45 Num qMax[maxn];
 46 Num qMin[maxn];
 47 int ans = INT_MAX;
 48 bool C(int m)
 49 {
 50     int frontMax,backMax;
 51     int frontMin,backMin;
 52     frontMax = 1,backMax = 0;
 53     frontMin = 1,backMin = 0;
 54     _for(i,1,n+1)
 55     {
 56         while(frontMax <= backMax && qMax[frontMax].x + m < in[i].x)
 57             frontMax ++;
 58 
 59         while(frontMin <= backMin && qMin[frontMin].x + m < in[i].x)
 60             frontMin ++;
 61 
 62         while(frontMax <= backMax && qMax[backMax].y <= in[i].y)
 63             backMax --;
 64         backMax ++;
 65         qMax[backMax].y = in[i].y;
 66         qMax[backMax].x = in[i].x;
 67 
 68         while(frontMin <= backMin && qMin[backMin].y >= in[i].y)
 69             backMin --;
 70         backMin ++;
 71         qMin[backMin].y = in[i].y;
 72         qMin[backMin].x = in[i].x;
 73         
 74         int dd = qMax[frontMax].y-qMin[frontMin].y;
 75         if(dd >= d)
 76             return true;
 77     }
 78     return false;
 79 }
 80 int solve()
 81 {
 82     int lb = in[1].x,ub = in[n].x+5;
 83     while(lb < ub)
 84     {
 85         int mid =  (lb+ub)/2;        
 86         if(C(mid)) ub = mid;
 87         else lb = mid+1;
 88     }
 89     return lb;
 90 }
 91 int main()
 92 {
 93 //    freopen("testdata (4).in","r+",stdin);
 94     n = read(),d = read();
 95     _for(i,1,n+1)
 96     {
 97         in[i].x = read();
 98         in[i].y = read();
 99     }
100 
101     sort(in+1,in+n+1);
102     ans = solve();
103     if(ans > in[n].x)
104         write(-1);
105     else
106         write(ans); 
107     return 0;
108 }

 

posted @ 2019-10-15 17:17  Asurudo  阅读(273)  评论(0编辑  收藏  举报