1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include<iostream> #include<algorithm> #include<cmath> #include<deque> #include<fstream> using namespace std;
struct posion{ int x,y; };
bool cmp(posion a,posion b){ return a.x<b.x; }
posion seq[200010];
int main(){ ios::sync_with_stdio(false); cin.tie(0);
int n,L; cin>>n>>L; for(int i=0;i<n;i++){ cin>>seq[i].x; } for(int i=0;i<n;i++){ cin>>seq[i].y; } sort(seq,seq+n,cmp);
int best=0; deque<int> max,min; for(int i=0,j=0;i<n;i++){ while(!max.empty() && seq[i].x-seq[max.back()].x>L) max.pop_back(); while(!min.empty() && seq[i].x-seq[min.back()].x>L) min.pop_back(); while(!max.empty() && seq[i].y>seq[max.back()].y) max.pop_front(); while(!min.empty() && seq[i].y<seq[min.back()].y) min.pop_front(); max.push_front(i); min.push_front(i);
if(!max.empty() && !min.empty() && abs(seq[max.back()].y-seq[min.back()].y)>best){ best=abs(seq[max.back()].y-seq[min.back()].y); } } cout<<best; return 0; }
|