#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
const int N=3e5+5;
const int INF=INT_MAX/2;
int n;
int a[3*N],b[N],c[N],pos[3*N];
bool lo[3*N],d1[3*N],d2[3*N];
struct Info{
int sum,pre,suf,mx;
static Info unit(){
return {0,-INF,-INF,-INF};
}
friend Info operator+(const Info &l,const Info &r){
return {l.sum+r.sum,max(l.pre,l.sum+r.pre),max(r.suf,r.sum+l.suf),max({l.mx,r.mx,l.suf+r.pre})};
}
};
struct Stack{
using P = pair<Info,int>;
stack<P> s;
stack<Info> p;
bool empty(){return s.empty();}
P front(){return s.top();}
P back(){return s.top();}
void push_front(Info x,int v){
s.emplace(x,v);
if(p.empty())p.emplace(x);
else p.emplace(x+p.top());
}
void push_back(Info x,int v){
s.emplace(x,v);
if(p.empty())p.emplace(x);
else p.emplace(p.top()+x);
}
void push_front(P x){push_front(x.first,x.second);}
void push_back(P x){push_back(x.first,x.second);}
void pop_back(){s.pop();p.pop();}
void pop_front(){s.pop();p.pop();}
Info query(){return p.empty()?Info::unit():p.top();}
};
struct Deque{
using P = pair<Info,int>;
deque<P> dq;
stack<Info> l,r;
void rebalance(bool left){
int s=dq.size();
int m=(s+left)/2;
while(!l.empty())l.pop();
while(!r.empty())r.pop();
{
Info sum=Info::unit();
for(int i=m-1;i>=0;i--){
sum=dq[i].first+sum;
l.emplace(sum);
}
}
{
Info sum=Info::unit();
for(int i=m;i<s;i++){
sum=sum+dq[i].first;
r.emplace(sum);
}
}
}
bool empty(){return dq.empty();}
int size(){return dq.size();};
P front(){return dq.front();}
P back(){return dq.back();}
void push_front(Info x,int v){
dq.emplace_front(x,v);
if(l.empty())l.emplace(x);
else l.emplace(x+l.top());
}
void push_back(Info x,int v){
dq.emplace_back(x,v);
if(r.empty())r.emplace(x);
else r.emplace(r.top()+x);
}
void push_front(P x){push_front(x.first,x.second);}
void push_back(P x){push_back(x.first,x.second);}
void pop_front(){
assert(!dq.empty());
if(l.empty())rebalance(true);
dq.pop_front();
l.pop();
}
void pop_back(){
assert(!dq.empty());
if(r.empty())rebalance(false);
dq.pop_back();
r.pop();
}
Info query(){
if(dq.empty())return Info::unit();
if(l.empty())return r.top();
if(r.empty())return l.top();
return l.top()+r.top();
}
};
void calc(int *b,bool *d,int k){
int st=0,ed=0;
Stack q1,q3;
Deque q2;
auto work=[&](int i){
if(lo[i]){
while(!q1.empty()&&q1.back().second>=pos[i]){
q2.push_front(q1.back());
q1.pop_back();
}
while(!q2.empty()&&q2.front().second<pos[i]){
q1.push_back(q2.front());
q2.pop_front();
}
while(!q3.empty()&&q3.front().second<pos[i]){
q1.push_back(q3.front());
q3.pop_front();
}
}else{
while(!q3.empty()&&q3.front().second<=pos[i]){
q2.push_back(q3.front());
q3.pop_front();
}
while(!q2.empty()&&q2.back().second>pos[i]){
q3.push_front(q2.back());
q2.pop_back();
}
while(!q1.empty()&&q1.back().second>pos[i]){
q3.push_front(q1.back());
q1.pop_back();
}
}
};
for(int i=0;i<3*n;i++){
while(st<n&&b[st]<a[i]-k)st++;
while(st>0&&b[st-1]>=a[i]-k)st--;
while(ed<n&&b[ed]<=a[i]+k)ed++;
while(ed>0&&b[ed-1]>a[i]+k)ed--;
work(i);
Info cur{1,1-ed,1+st,1+st-ed};
if(lo[i])q2.push_front(cur,pos[i]);
else q2.push_back(cur,pos[i]);
if(i>=n){
work(i-n);
if(lo[i-n])q2.pop_front();
else q2.pop_back();
d[i]=(q1.query()+q2.query()+q3.query()).mx<=0;
}
}
}
bool check(int k){
calc(b,d1,k);
calc(c,d2,k);
for(int i=n;i<2*n;i++){
if((d1[i]&&d2[i+n])||(d2[i]&&d1[i+n])){
return true;
}
}
return false;
}
int main(){
cin >> n;
vector<pair<int,int>> vals;
for(int i=0;i<2*n;i++){
cin >> a[i];
vals.emplace_back(a[i],i>n?~i:i);
}
sort(vals.begin(),vals.end());
for(int i=0;i<2*n;i++){
int j=vals[i].second;
pos[j<0?~j:j]=i;
}
for(int i=2*n;i<3*n;i++){
a[i]=a[i-2*n];
pos[i]=pos[i-2*n];
}
for(int i=0;i<2*n;i++){
lo[i]=pos[i]<pos[i+n];
}
for(int i=2*n;i<3*n;i++){
lo[i]=lo[i-2*n];
}
for(int i=0;i<n;i++){
cin >> b[i];
}
for(int i=0;i<n;i++){
cin >> c[i];
}
sort(b,b+n);
sort(c,c+n);
int l=0,r=1'000'000'000;
while(l<r){
int m=(l+r)/2;
if(check(m))r=m;
else l=m+1;
}
cout << l << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
29 ms |
592 KB |
Output is correct |
28 |
Correct |
22 ms |
696 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
31 ms |
592 KB |
Output is correct |
31 |
Correct |
28 ms |
592 KB |
Output is correct |
32 |
Correct |
20 ms |
592 KB |
Output is correct |
33 |
Correct |
8 ms |
336 KB |
Output is correct |
34 |
Correct |
25 ms |
592 KB |
Output is correct |
35 |
Correct |
24 ms |
760 KB |
Output is correct |
36 |
Correct |
31 ms |
776 KB |
Output is correct |
37 |
Correct |
19 ms |
696 KB |
Output is correct |
38 |
Correct |
37 ms |
592 KB |
Output is correct |
39 |
Correct |
25 ms |
592 KB |
Output is correct |
40 |
Correct |
22 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4366 ms |
28832 KB |
Output is correct |
2 |
Correct |
4386 ms |
28352 KB |
Output is correct |
3 |
Correct |
3623 ms |
30492 KB |
Output is correct |
4 |
Correct |
4227 ms |
40420 KB |
Output is correct |
5 |
Correct |
4398 ms |
36504 KB |
Output is correct |
6 |
Correct |
145 ms |
1672 KB |
Output is correct |
7 |
Correct |
3628 ms |
40872 KB |
Output is correct |
8 |
Correct |
3371 ms |
35740 KB |
Output is correct |
9 |
Correct |
4080 ms |
40380 KB |
Output is correct |
10 |
Correct |
4408 ms |
40256 KB |
Output is correct |
11 |
Correct |
4608 ms |
40000 KB |
Output is correct |
12 |
Correct |
3993 ms |
38104 KB |
Output is correct |
13 |
Correct |
3935 ms |
36912 KB |
Output is correct |
14 |
Correct |
3462 ms |
40444 KB |
Output is correct |
15 |
Correct |
3358 ms |
36888 KB |
Output is correct |
16 |
Correct |
3320 ms |
36768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
29 ms |
592 KB |
Output is correct |
28 |
Correct |
22 ms |
696 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
31 ms |
592 KB |
Output is correct |
31 |
Correct |
28 ms |
592 KB |
Output is correct |
32 |
Correct |
20 ms |
592 KB |
Output is correct |
33 |
Correct |
8 ms |
336 KB |
Output is correct |
34 |
Correct |
25 ms |
592 KB |
Output is correct |
35 |
Correct |
24 ms |
760 KB |
Output is correct |
36 |
Correct |
31 ms |
776 KB |
Output is correct |
37 |
Correct |
19 ms |
696 KB |
Output is correct |
38 |
Correct |
37 ms |
592 KB |
Output is correct |
39 |
Correct |
25 ms |
592 KB |
Output is correct |
40 |
Correct |
22 ms |
592 KB |
Output is correct |
41 |
Correct |
4366 ms |
28832 KB |
Output is correct |
42 |
Correct |
4386 ms |
28352 KB |
Output is correct |
43 |
Correct |
3623 ms |
30492 KB |
Output is correct |
44 |
Correct |
4227 ms |
40420 KB |
Output is correct |
45 |
Correct |
4398 ms |
36504 KB |
Output is correct |
46 |
Correct |
145 ms |
1672 KB |
Output is correct |
47 |
Correct |
3628 ms |
40872 KB |
Output is correct |
48 |
Correct |
3371 ms |
35740 KB |
Output is correct |
49 |
Correct |
4080 ms |
40380 KB |
Output is correct |
50 |
Correct |
4408 ms |
40256 KB |
Output is correct |
51 |
Correct |
4608 ms |
40000 KB |
Output is correct |
52 |
Correct |
3993 ms |
38104 KB |
Output is correct |
53 |
Correct |
3935 ms |
36912 KB |
Output is correct |
54 |
Correct |
3462 ms |
40444 KB |
Output is correct |
55 |
Correct |
3358 ms |
36888 KB |
Output is correct |
56 |
Correct |
3320 ms |
36768 KB |
Output is correct |
57 |
Correct |
4054 ms |
39872 KB |
Output is correct |
58 |
Correct |
4516 ms |
36608 KB |
Output is correct |
59 |
Correct |
2950 ms |
28656 KB |
Output is correct |
60 |
Correct |
4838 ms |
40148 KB |
Output is correct |
61 |
Correct |
4266 ms |
36508 KB |
Output is correct |
62 |
Correct |
3362 ms |
33128 KB |
Output is correct |
63 |
Correct |
104 ms |
1536 KB |
Output is correct |
64 |
Correct |
3649 ms |
40728 KB |
Output is correct |
65 |
Correct |
3730 ms |
35488 KB |
Output is correct |
66 |
Correct |
4959 ms |
40180 KB |
Output is correct |
67 |
Correct |
2975 ms |
40340 KB |
Output is correct |
68 |
Correct |
3487 ms |
39988 KB |
Output is correct |
69 |
Correct |
2342 ms |
27136 KB |
Output is correct |
70 |
Correct |
3524 ms |
40236 KB |
Output is correct |
71 |
Correct |
2894 ms |
40348 KB |
Output is correct |
72 |
Correct |
3524 ms |
40208 KB |
Output is correct |
73 |
Correct |
3536 ms |
40092 KB |
Output is correct |
74 |
Correct |
4339 ms |
40380 KB |
Output is correct |
75 |
Correct |
3055 ms |
27116 KB |
Output is correct |
76 |
Correct |
4770 ms |
40280 KB |
Output is correct |
77 |
Correct |
3260 ms |
36164 KB |
Output is correct |