Submission #70668583
Source Code Expand
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
int n;
cin >> n;
ll inf = 1e17;
map<ll, ll> Dist;
Dist[0] = inf;
ll ans = inf;
while(n--){
ll x;
cin >> x;
auto next = Dist.lower_bound(x);
auto prev = next; prev--;
// cout << "prev " << (*prev).first << " " << (*prev).second << endl;
// cout << "next " << (*next).first << " " << (*next).second << endl;
if(next == Dist.end()){
ll d_prev = x - (*prev).first;
ans += d_prev;
Dist[x] = d_prev;
if(d_prev < (*prev).second){
ans -= (*prev).second;
ans += d_prev;
Dist[(*prev).first] = d_prev;
}
}else{
ll d_prev = x - (*prev).first;
ll d_next = (*next).first - x;
ans += min(d_prev, d_next);
Dist[x] = min(d_prev, d_next);
if(d_prev < (*prev).second){
ans -= (*prev).second;
ans += d_prev;
Dist[(*prev).first] = d_prev;
}
if(d_next < (*next).second){
ans -= (*next).second;
ans += d_next;
Dist[(*next).first] = d_next;
}
}
// for(auto [key, value] : Dist) cout << key << " " << value << endl;
cout << ans << endl;
// cout << endl;
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Neighbor Distance |
| User | sakimori_coder |
| Language | C++23 (GCC 15.2.0) |
| Score | 400 |
| Code Size | 1526 Byte |
| Status | AC |
| Exec Time | 761 ms |
| Memory | 36928 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt |
| All | sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| sample_01.txt | AC | 1 ms | 3516 KiB |
| test_01.txt | AC | 1 ms | 3380 KiB |
| test_02.txt | AC | 1 ms | 3512 KiB |
| test_03.txt | AC | 578 ms | 36740 KiB |
| test_04.txt | AC | 577 ms | 36756 KiB |
| test_05.txt | AC | 576 ms | 36700 KiB |
| test_06.txt | AC | 658 ms | 36700 KiB |
| test_07.txt | AC | 659 ms | 36744 KiB |
| test_08.txt | AC | 658 ms | 36908 KiB |
| test_09.txt | AC | 611 ms | 36768 KiB |
| test_10.txt | AC | 598 ms | 36732 KiB |
| test_11.txt | AC | 613 ms | 36728 KiB |
| test_12.txt | AC | 598 ms | 36764 KiB |
| test_13.txt | AC | 619 ms | 36680 KiB |
| test_14.txt | AC | 627 ms | 36760 KiB |
| test_15.txt | AC | 617 ms | 36868 KiB |
| test_16.txt | AC | 629 ms | 36724 KiB |
| test_17.txt | AC | 757 ms | 36764 KiB |
| test_18.txt | AC | 753 ms | 36780 KiB |
| test_19.txt | AC | 750 ms | 36812 KiB |
| test_20.txt | AC | 751 ms | 36928 KiB |
| test_21.txt | AC | 752 ms | 36860 KiB |
| test_22.txt | AC | 750 ms | 36724 KiB |
| test_23.txt | AC | 749 ms | 36892 KiB |
| test_24.txt | AC | 745 ms | 36816 KiB |
| test_25.txt | AC | 748 ms | 36924 KiB |
| test_26.txt | AC | 750 ms | 36816 KiB |
| test_27.txt | AC | 761 ms | 36736 KiB |
| test_28.txt | AC | 752 ms | 36752 KiB |
| test_29.txt | AC | 754 ms | 36908 KiB |
| test_30.txt | AC | 757 ms | 36736 KiB |