# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
891079 | vjudge1 | Bitaro's travel (JOI23_travel) | C++17 | 714 ms | 547136 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl "\n"
#define ll long long
#define int ll
int E,B;
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const ll N=5e5+5,INF=1e18,mod=1e9+7;
vector<vector<int>> pr(N,vector<int>(65,0)),
nx(N,vector<int>(65,0));
void solve(){
int n;cin>>n;
vector<int> x(n+2);
for(int i=1;i<=n;i++) cin>>x[i];
x[0]=-INF;
x[n+1]=INF;
for(int i=0;i<=55;i++) pr[1][i]=1;
for(int i=2;i<=n;i++){
pr[i]=pr[i-1];
for(int j=0;j<=55;j++){
if((1<<j)<=x[i]-x[i-1])
pr[i][j]=i;
}
}
for(int i=0;i<=55;i++) nx[n][i]=n;
for(int i=n-1;i>=1;i--){
nx[i]=nx[i+1];
for(int j=0;j<=55;j++){
if((1<<j)<=x[i+1]-x[i])
nx[i][j]=i;
}
}
int q,l,r,ans;cin>>q;
while(q--){
int a;cin>>a;
int pos=upper_bound(all(x),a)-x.begin();
if(a-x[pos-1]<=x[pos]-a) pos--;
ans=abs(a-x[pos]);
l=r=pos;
bool s=(x[pos]-x[pos-1]<=x[pos+1]-x[pos]);
int k=0;
while(l>1 || r<n){
while((1<<(k+1))<=(x[r]-x[l])) k++;
if(s){
ans+=x[l]-x[pr[l][k]];
l=pr[l][k];
if(x[l]-x[l-1]<=x[r+1]-x[l] && l>1){
ans+=x[l]-x[l-1];l--;
}
else if(r<n){
ans+=x[r]-x[l];
s=0;
}
}
else{
ans+=x[nx[r][k]]-x[r];
r=nx[r][k];
if(x[r+1]-x[r]<=x[r]-x[l-1] && r<n){
ans+=x[r+1]-x[r];r++;
}
else if(l>1){
ans+=x[r]-x[l];
s=1;
}
}
}
cout<<ans<<endl;
}
}
main(){
B=clock();
ios;
int T=1;
//cin>>T;
for(int i=1;i<=T;i++){
//cout<<"Case #"<<i<<": ";
solve();
}
E=clock();
cerr<<"\n\n\n\nTIME: "<<((float)E-B)/CLOCKS_PER_SEC<<" SECONDS";
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |