Submission #1117257

# Submission time Handle Problem Language Result Execution time Memory
1117257 2024-11-23T06:16:23 Z hqminhuwu Bitaro's travel (JOI23_travel) C++14
100 / 100
389 ms 8908 KB
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double fl;
typedef vector <int> vi;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;

#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define fofr(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"

template<class X, class Y>
	bool minz(X &x, const Y &y) {
		if (x > y) {
			x = y;
			return true;
		} return false;
	}
template<class X, class Y>
	bool maxz(X &x, const Y &y) {
		if (x < y) {
			x = y;
			return true;
		} return false;
	}

const int N = 5e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;

ll n, u, q, s;
vector <ll> x;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	#ifdef kaguya
		freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
	#endif

	cin >> n;

	forr (i, 1, n){
		cin >> u;
		x.pb(u);
	}

	cin >> q;

	while(q--) {
		cin >> s;
		
		ll i = lower_bound(all(x), s) - x.begin(), ans=0;
		
		if(!i){
			cout << x.back() - s << "\n";
			continue;
		}

		if (i == n){
			cout << s - x[0] << "\n";
			continue;
		}

		ll fr = x[i] - s, fl = s - x[i - 1], l, r;
		
		if (fl <= fr){
			ans += fl;
			l = r = i - 1;
			s = x[i - 1];
		} else {
			ans += fr;
			l = r = i;
			s = x[i];
		}

		while (1){
			if (!l){
				ans += x[n - 1] - s;
				break;
			}

			if (r == n - 1){
				ans += s - x[0];
				break;
			}

			if (s - x[l - 1] <= x[r + 1] - s){
				ll i = lower_bound(all(x), 2 * s - x[r + 1]) - x.begin();
				ans += s - x[i];
				s = x[i];
				l = i;
			} else {
				ll i = lower_bound(all(x), 2 * s - x[l - 1]) - x.begin() - 1;
				ans += x[i] - s;
				s = x[i];
				r = i;
			}
		}

		cout << ans << "\n";
	}


	return 0;
}
/*



*/

# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 22 ms 4828 KB Output is correct
18 Correct 17 ms 5068 KB Output is correct
19 Correct 16 ms 5008 KB Output is correct
20 Correct 19 ms 4816 KB Output is correct
21 Correct 16 ms 4812 KB Output is correct
22 Correct 16 ms 4824 KB Output is correct
23 Correct 16 ms 5068 KB Output is correct
24 Correct 16 ms 4848 KB Output is correct
25 Correct 16 ms 5068 KB Output is correct
26 Correct 16 ms 5080 KB Output is correct
27 Correct 16 ms 5040 KB Output is correct
28 Correct 16 ms 5048 KB Output is correct
29 Correct 15 ms 5224 KB Output is correct
30 Correct 18 ms 5068 KB Output is correct
31 Correct 22 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 317 ms 6332 KB Output is correct
8 Correct 324 ms 6484 KB Output is correct
9 Correct 321 ms 6336 KB Output is correct
10 Correct 337 ms 6336 KB Output is correct
11 Correct 333 ms 6556 KB Output is correct
12 Correct 322 ms 6336 KB Output is correct
13 Correct 30 ms 4124 KB Output is correct
14 Correct 19 ms 1616 KB Output is correct
15 Correct 216 ms 7532 KB Output is correct
16 Correct 208 ms 7616 KB Output is correct
17 Correct 237 ms 7616 KB Output is correct
18 Correct 29 ms 4168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 22 ms 4828 KB Output is correct
18 Correct 17 ms 5068 KB Output is correct
19 Correct 16 ms 5008 KB Output is correct
20 Correct 19 ms 4816 KB Output is correct
21 Correct 16 ms 4812 KB Output is correct
22 Correct 16 ms 4824 KB Output is correct
23 Correct 16 ms 5068 KB Output is correct
24 Correct 16 ms 4848 KB Output is correct
25 Correct 16 ms 5068 KB Output is correct
26 Correct 16 ms 5080 KB Output is correct
27 Correct 16 ms 5040 KB Output is correct
28 Correct 16 ms 5048 KB Output is correct
29 Correct 15 ms 5224 KB Output is correct
30 Correct 18 ms 5068 KB Output is correct
31 Correct 22 ms 5068 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 317 ms 6332 KB Output is correct
39 Correct 324 ms 6484 KB Output is correct
40 Correct 321 ms 6336 KB Output is correct
41 Correct 337 ms 6336 KB Output is correct
42 Correct 333 ms 6556 KB Output is correct
43 Correct 322 ms 6336 KB Output is correct
44 Correct 30 ms 4124 KB Output is correct
45 Correct 19 ms 1616 KB Output is correct
46 Correct 216 ms 7532 KB Output is correct
47 Correct 208 ms 7616 KB Output is correct
48 Correct 237 ms 7616 KB Output is correct
49 Correct 29 ms 4168 KB Output is correct
50 Correct 247 ms 8908 KB Output is correct
51 Correct 221 ms 8648 KB Output is correct
52 Correct 217 ms 8640 KB Output is correct
53 Correct 389 ms 8640 KB Output is correct
54 Correct 380 ms 8640 KB Output is correct
55 Correct 373 ms 8640 KB Output is correct
56 Correct 53 ms 8384 KB Output is correct
57 Correct 61 ms 8384 KB Output is correct
58 Correct 51 ms 8512 KB Output is correct