Submission #1247371

#TimeUsernameProblemLanguageResultExecution timeMemory
1247371khoianhBitaro's travel (JOI23_travel)C++20
100 / 100
371 ms4132 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mn = 2e5 + 5;
ll n, q, a[mn], x, l, r, p;

void solve(){
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> a[i];
	cin >> q;
	while(q--){
		ll ans = 0;
		cin >> x;
		p = lower_bound(a + 1, a + n + 1, x) - a;
		if(p > n) l = r = n, ans += x - a[n];
		else if (p == 1) l = r = 1, ans += a[1] - x;
		else if(a[p] - x < x - a[p - 1]) l = r = p, ans += a[p] - x;
		else l = r = p - 1, ans += x - a[p - 1];
		x = l;
		while(true){
			if(l == 1){
				ans += a[n] - a[x];
				break;
			}
			if(r == n){
				ans += a[x] - a[1];
				break;
			}
//			cout << l << " " << r << "\n";
			if(a[x] - a[l - 1] <= a[r + 1] - a[x]){
				p = lower_bound(a + 1, a + n + 1, 2 * a[x] - a[r + 1]) - a;
				ans += a[x] - a[p];
				x = p;
				l = p;
			} else {
				p = upper_bound(a + 1, a + n + 1, 2 * a[x] - a[l - 1]) - a - 1;
				ans += a[p] - a[x];
				x = p;
				r = p;
			}
//			cout << l << " " << r << " " << x << " " << ans << "\n";
		}
		cout << ans << "\n";
	}
    return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	if(fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	int testCase = 1;
	//cin >> testCase;
	while(testCase--) solve();
}

Compilation message (stderr)

travel.cpp: In function 'int main()':
travel.cpp:52:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
travel.cpp:53:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |                 freopen(".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...