# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1247312 | BuiDucManh123 | Bitaro's travel (JOI23_travel) | C++20 | 324 ms | 3432 KiB |
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
#define ld long double
const int mod = 1e9+7;
using namespace std;
int a[200009];
signed main() {
if (fopen(taskname".inp","r")) {
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int q;
cin >> q;
while(q--){
int s;
cin >> s;
int j = lower_bound(a + 1, a + n + 1, s) - a;
int l = j;
int r = j;
if(j == n + 1) l = r = n;
else if(a[j] == s || j == 1) l = r = j;
else if(a[j] - s < s - a[j - 1]) l = r = j;
else l = r = j - 1;
int ans = abs(s - a[l]);
s = a[l];
while(true){
if(l == 1){
ans += abs(s - a[n]);
break;
}
if(r == n){
ans += abs(s - a[1]);
break;
}
int L = a[l - 1], R = a[r + 1];
if(s - L <= R - s){
int nxt = lower_bound(a + 1, a + n + 1, 2 * s-R)-a;
ans += abs(s - a[nxt]);
s = a[nxt];
l = nxt;
}else{
int nxt = upper_bound(a + 1, a + n + 1, 2 * s-L)-a-1;
ans += abs(s - a[nxt]);
s = a[nxt];
r = nxt;
}
}
cout << ans << "\n";
}
return 0;
}
Compilation message (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... |