This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shortcut.h"
#include <bits/stdc++.h>
#define ll long long
// #define int ll
#define pii pair<int,int>
#define F first
#define S second
#define sz(s) (int)s.size()
#define in insert
#define pb push_back
#define all(v) v.begin(),v.end()
#define mem(a,i) memset(a,i,sizeof(a))
using namespace std;
const int MAX=2e5+10;
const ll inf=1e18;
int n,c;
ll p[MAX];
pair<ll,ll> a[MAX],b[MAX];
vector<int> d;
bool check(ll L){
ll sum[2][2]={{-inf,inf},{-inf,inf}};
ll sum1[2][2]={{-inf,inf},{-inf,inf}};
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)continue;
if(p[j]-p[i]+d[i]+d[j]<=L)continue;
sum1[0][0]=max(sum1[0][0],p[i]+p[j]+d[i]+d[j]-L+c);
sum1[1][1]=min(sum1[1][1],p[i]-d[i]+p[j]-d[j]+L-c);
sum1[1][0]=max(sum1[1][0],p[j]+d[j]-p[i]+d[i]-L+c);
sum1[0][1]=min(sum1[0][1],p[j]-d[j]-p[i]-d[i]+L-c);
}
}
{
int r=-1;
pair<ll,ll> mx={-inf,-inf},mx1={-inf,-inf};
for(int i=0;i<n;i++){
while(r+1<n&&b[r+1].F<a[i].F-L){
pair<ll,ll> bf={a[b[r+1].S].F,b[r+1].S};
if(mx<bf){
mx1=mx;
mx=bf;
}
else if(mx1<bf){
mx1=bf;
}
r++;
}
if(i!=mx.S)sum[0][0]=max(sum[0][0],mx.F+a[i].F+c-L);
else sum[0][0]=max(sum[0][0],mx1.F+a[i].F+c-L);
}
}
{
int r=-1;
pair<ll,ll> mn={inf,inf},mn1={inf,inf};
for(int i=0;i<n;i++){
while(r+1<n&&b[r+1].F<a[i].F-L){
pair<ll,ll> bf={-a[b[r+1].S].F,b[r+1].S};
if(mn>bf){
mn1=mn;
mn=bf;
}
else if(mn1>bf){
mn1=bf;
}
r++;
}
if(i!=mn.S)sum[0][1]=min(sum[0][1],L-c+a[i].S+mn.F);
else sum[0][1]=min(sum[0][1],L-c+a[i].S+mn1.F);
}
}
{
int r=-1;
pair<ll,ll> mn={inf,inf},mn1={inf,inf};
for(int i=0;i<n;i++){
while(r+1<n&&b[r+1].F<a[i].F-L){
pair<ll,ll> bf={b[r+1]};
if(bf<mn){
mn1=mn;
mn=bf;
}
else if(bf<mn1){
mn1=bf;
}
r++;
}
if(mn.S!=i)sum[1][1]=min(sum[1][1],mn.F+a[i].S+L-c);
if(mn1.S!=i)sum[1][1]=min(sum[1][1],mn1.F+a[i].S+L-c);
}
}
{
int r=-1;
pair<ll,ll> mx={-inf,-inf},mx1={-inf,-inf};
for(int i=0;i<n;i++){
while(r+1<n&&b[r+1].F<a[i].F-L){
pair<ll,ll> bf={-b[r+1].F,b[r+1].S};
if(bf>mx){
mx1=mx;
mx=bf;
}
else if(bf>mx1){
mx1=bf;
}
r++;
}
if(i!=mx.S)sum[1][0]=max(sum[1][0],a[i].F+mx.F+c-L);
else sum[1][0]=max(sum[1][0],a[i].F+mx1.F+c-L);
}
}
// cout<<sum[0][0]<<" "<<sum[1][1]<<" "<<sum[1][0]<<" "<<sum[0][1]<<"\n";
// cout<<sum1[0][0]<<" "<<sum1[1][1]<<" "<<sum1[1][0]<<" "<<sum1[0][1]<<"\n";
int r=-1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)continue;
if(sum[0][0]<=p[i]+p[j]&&p[i]+p[j]<=sum[1][1]&&sum[1][0]<=p[j]-p[i]&&p[j]-p[i]<=sum[0][1])return 1;
}
// while(r+1<i&&!(max(sum[1][1]+p[i],sum[0][0]-p[i])<=p[r]&&p[r]<=min(sum[1][0]-p[i],sum[0][1]+p[i])))r++;
// if(r>=0&&max(sum[1][1]+p[i],sum[0][0]-p[i])<=p[r]&&p[r]<=min(sum[1][0]-p[i],sum[0][1]+p[i])){
// return 1;
// }
}
return 0;
}
long long find_shortcut(int N, vector<int> G, vector<int> D, int C){
n=N;
c=C;
d=D;
for(int i=1;i<n;i++)p[i]=p[i-1]+G[i-1];
for(int i=0;i<n;i++){
// cout<<p[i]+d[i]<<"\n";
a[i]={p[i]+d[i],p[i]-d[i]};
}
sort(a,a+n);
for(int i=0;i<n;i++){
b[i]={a[i].S,i};
}
sort(b,b+n);
// cout<<check(51)<<"\n";
// return 110;
ll l=0,r=a[n-1].F-b[0].F-1,res=a[n-1].F-b[0].F;
while(l<=r){
ll m=(l+r)/2;
if(check(m)){
r=m-1;
res=m;
}
else{
l=m+1;
}
}
return res;
}
Compilation message (stderr)
shortcut.cpp: In function 'bool check(long long int)':
shortcut.cpp:116:9: warning: unused variable 'r' [-Wunused-variable]
116 | int r=-1;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |