| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1314732 | wedonttalkanymore | Bitaro's travel (JOI23_travel) | C++20 | 15 ms | 3568 KiB |
#include <bits/stdc++.h>
/*
Checklist:
- Check statement:
- Check filename:
- Check test limit:
- Stresstest:
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, a[N];
int q;
int s[N];
int pfs[N];
int solve(int x) {
int ans = 0;
int pos = lower_bound(a + 1, a + n + 1, x) - a;
pos = min(pos, n);
int now;
int L, R;
if (pos > 1 && abs(x - a[pos - 1]) <= abs(x - a[pos])) {
ans += abs(x - a[pos - 1]);
L = R = pos - 1;
now = 1;
}
else {
ans += abs(x - a[pos]);
L = R = pos;
now = 2;
}
while (L > 1 && R < n) {
if (now == 1) { // dang o L
int val = a[R + 1] - a[L];
int p = lower_bound(a + 1, a + n + 1, a[L] - val) - a;
if (p < L) {
ans += a[L] - a[p];
L = p;
}
else {
ans += val;
R++;
now = 2;
}
}
else {
int val = a[R] - a[L - 1];
int p = upper_bound(a + 1, a + n + 1, a[R] + val) - a - 1;
if (p > R) {
ans += a[p] - a[R];
R = p;
}
else {
ans += val;
L--;
now = 1;
}
}
}
ans += a[n] - a[1];
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i <= n; i++) pfs[i] = pfs[i - 1] + a[i] - a[i - 1];
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> s[i];
cout << solve(s[i]) << '\n';
}
return 0;
}
컴파일 시 표준 에러 (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... | ||||
