Submission #1247312

#TimeUsernameProblemLanguageResultExecution timeMemory
1247312BuiDucManh123Bitaro's travel (JOI23_travel)C++20
100 / 100
324 ms3432 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)

travel.cpp: In function 'int main()':
travel.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         freopen(taskname".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen(taskname".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...