#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
#define ll long long
using namespace std;
int tt = 1, n;
ll a[200005], dist, x[200005];
set<pair<ll, int>> st;
bool ok = 0;
ll pref[200005], suff[200005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
st.insert({a[i], i});
if(i > 1 && a[i] - a[i - 1] > 100) ok = 1;
}
cin >> tt;
if(!ok){
for(int i = 2; i <= n; i++)
pref[i] = pref[i - 1] + (a[i] - a[i - 1]);
for(int i = n - 1; i >= 1; i--)
suff[i] = suff[i + 1] + (a[i + 1] - a[i]);
while(tt--){
ll pos, p1, p2, sum = 0;
cin >> pos;
deque<pair<ll, int>> d1, d2;
if(a[1] <= pos){
auto it = st.upper_bound({pos + 1, 0});
it--;
p1 = it->second;
for(int i = p1; i >= 1; i--)
if(abs(pos - a[i]) <= 100) d1.push_front({a[i], i});
}
if(a[n] > pos){
auto it = st.upper_bound({pos + 1, 0});
p2 = it->second;
for(int i = p2; i <= n; i++)
if(abs(a[i] - pos) <= 100) d2.push_back({a[i], i});
}
if(d1.empty() || d2.empty()){
if(pos <= a[1]) cout << pref[n] + a[1] - pos << "\n";
else cout << suff[1] + pos - a[n] << "\n";
continue;
}
int pos1 = d1.back().second, pos2 = d2.front().second;
int t = 0;
while(pos1 >= 1 && pos2 <= n){
if(abs(pos - a[pos1]) <= abs(pos - a[pos2])){
sum += abs(pos - a[pos1]);
pos = a[pos1];
pos1--;
}
else{
sum += abs(pos - a[pos2]);
pos = a[pos2];
pos2++;
}
if(pos1 < 1 || pos2 > n){
t = -1;
break;
}
if(a[pos2] - pos > 100){
t = 1;
break;
}
if(pos - a[pos1] > 100){
break;
}
}
if(t == -1){
if(pos1 == 0) cout << sum + (a[n] - pos) << "\n";
else cout << sum + (pos - a[1]) << "\n";
continue;
}
if(t == 1){
cout << sum + (pos1 - a[1]) + (a[n] - a[1]) << "\n";
continue;
}
else{
cout << sum + (a[n] - pos) + (a[n] - a[1]) << "\n";
continue;
}
}
}
for(int i = 1; i <= tt; i++){
cin >> x[i];
ll sum = 0, pos = x[i];
while(!st.empty()){
ll x1 = 1e10, y1 = 1e10;
pair<ll, int> p1, p2;
if(st.begin()->first <= pos){
auto it = st.lower_bound({pos, 0});
it--;
x1 = pos - it->first;
p1 = *it;
}
if(st.rbegin()->first > pos){
auto it = st.upper_bound({pos, 0});
y1 = it->first - pos;
p2 = *it;
}
if(x1 <= y1){
sum += x1;
pos -= x1;
st.erase(p1);
}
else{
sum += y1;
pos += y1;
st.erase(p2);
}
}
cout << sum << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2460 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2692 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2460 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2692 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
73 ms |
18516 KB |
Output is correct |
18 |
Correct |
93 ms |
18468 KB |
Output is correct |
19 |
Correct |
73 ms |
18256 KB |
Output is correct |
20 |
Correct |
75 ms |
18512 KB |
Output is correct |
21 |
Correct |
72 ms |
18512 KB |
Output is correct |
22 |
Correct |
88 ms |
18492 KB |
Output is correct |
23 |
Correct |
74 ms |
18512 KB |
Output is correct |
24 |
Correct |
70 ms |
18276 KB |
Output is correct |
25 |
Correct |
84 ms |
18516 KB |
Output is correct |
26 |
Correct |
103 ms |
18512 KB |
Output is correct |
27 |
Correct |
87 ms |
18644 KB |
Output is correct |
28 |
Correct |
83 ms |
18512 KB |
Output is correct |
29 |
Correct |
75 ms |
18256 KB |
Output is correct |
30 |
Correct |
88 ms |
18256 KB |
Output is correct |
31 |
Correct |
77 ms |
18256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Execution timed out |
3074 ms |
20640 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2460 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2692 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
73 ms |
18516 KB |
Output is correct |
18 |
Correct |
93 ms |
18468 KB |
Output is correct |
19 |
Correct |
73 ms |
18256 KB |
Output is correct |
20 |
Correct |
75 ms |
18512 KB |
Output is correct |
21 |
Correct |
72 ms |
18512 KB |
Output is correct |
22 |
Correct |
88 ms |
18492 KB |
Output is correct |
23 |
Correct |
74 ms |
18512 KB |
Output is correct |
24 |
Correct |
70 ms |
18276 KB |
Output is correct |
25 |
Correct |
84 ms |
18516 KB |
Output is correct |
26 |
Correct |
103 ms |
18512 KB |
Output is correct |
27 |
Correct |
87 ms |
18644 KB |
Output is correct |
28 |
Correct |
83 ms |
18512 KB |
Output is correct |
29 |
Correct |
75 ms |
18256 KB |
Output is correct |
30 |
Correct |
88 ms |
18256 KB |
Output is correct |
31 |
Correct |
77 ms |
18256 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Execution timed out |
3074 ms |
20640 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |