답안 #972493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972493 2024-04-30T13:49:53 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
45 / 100
457 ms 45552 KB
#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<ll> st;
bool ok = 0;
ll pref[200005], suff[200005];
map<ll, ll> mp;
set<pair<ll, ll>> st1;
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]);
        st1.insert({a[i], i});
        if(i > 1 && a[i] - a[i - 1] > 100) ok = 1;
        mp[a[i]] = i;
    }
    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;
            if(pos <= a[1]){
                cout << a[n] - pos << "\n";
                continue;
            }
            if(pos >= a[n]){
                cout << pos - a[1] << "\n";
                continue;
            }
            int pos2 = *st.lower_bound(pos);
            auto it = st.lower_bound(pos);
            it--;
            int pos1 = *it;
            int t = 0;
            bool f = 0;
            pos1 = mp[pos1];
            pos2 = mp[pos2];
            if(pos - a[pos1] <= a[pos2] - pos){
                sum = pos - a[pos1];
                pos = a[pos1];
                pos1--;
            }
            else{
                sum = a[pos2] - pos;
                pos = a[pos2];
                pos2++;
                f = 1;
            }
            while(pos1 >= 1 && pos2 <= n && a[pos2] - pos <= 100 && pos - a[pos1] <= 100){
                if(abs(pos - a[pos1]) <= abs(pos - a[pos2])){
                    sum += abs(pos - a[pos1]);
                    pos = a[pos1];
                    pos1--;
                    f = 0;
                }
                else{
                    sum +=  abs(pos - a[pos2]);
                    pos = a[pos2];
                    pos2++;
                    f = 1;
                }
            }
            if(!f){
                if(pos1 > 0) sum += (pos - a[1]);
                if(pos2 != n + 1) sum += (a[n] - a[1]); 
            }
            else{
                if(pos2 <= n) sum += (a[n] - pos);
                if(pos1 > 0) sum += (a[n] - a[1]);
            }
            cout << sum << "\n";
        }
    }
    else{
        for(int i = 1; i <= tt; i++){
            cin >> x[i];
            ll sum = 0, pos = x[i];
            while(!st1.empty()){
                ll x1 = 1e10, y1 = 1e10;
                pair<ll, int> p1, p2;
                if(st1.begin()->first <= pos){
                    auto it = st1.lower_bound({pos, 0});
                    it--;
                    x1 = pos - it->first;
                    p1 = *it;
                }
                if(st1.rbegin()->first > pos){
                    auto it = st1.upper_bound({pos, 0});
                    y1 = it->first - pos;
                    p2 = *it;
                }
                if(x1 <= y1){
                    sum += x1;
                    pos -= x1;
                    st1.erase(p1);
                }   
                else{
                    sum += y1;
                    pos += y1;
                    st1.erase(p2);
                }
            }
            cout << sum << "\n";
        }
    }
}

Compilation message

travel.cpp: In function 'int main()':
travel.cpp:55:21: warning: unused variable 'p1' [-Wunused-variable]
   55 |             ll pos, p1, p2, sum = 0;
      |                     ^~
travel.cpp:55:25: warning: unused variable 'p2' [-Wunused-variable]
   55 |             ll pos, p1, p2, sum = 0;
      |                         ^~
travel.cpp:69:17: warning: unused variable 't' [-Wunused-variable]
   69 |             int t = 0;
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 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 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2788 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 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 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 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2788 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 148 ms 39764 KB Output is correct
18 Correct 157 ms 39764 KB Output is correct
19 Correct 160 ms 39892 KB Output is correct
20 Correct 171 ms 39760 KB Output is correct
21 Correct 175 ms 39016 KB Output is correct
22 Correct 167 ms 38544 KB Output is correct
23 Correct 143 ms 38468 KB Output is correct
24 Correct 174 ms 38488 KB Output is correct
25 Correct 183 ms 38500 KB Output is correct
26 Correct 183 ms 38368 KB Output is correct
27 Correct 155 ms 38452 KB Output is correct
28 Correct 153 ms 38484 KB Output is correct
29 Correct 162 ms 38496 KB Output is correct
30 Correct 154 ms 38360 KB Output is correct
31 Correct 163 ms 38468 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 2 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 443 ms 42084 KB Output is correct
8 Correct 450 ms 44612 KB Output is correct
9 Correct 408 ms 44924 KB Output is correct
10 Correct 437 ms 44636 KB Output is correct
11 Correct 457 ms 44508 KB Output is correct
12 Correct 398 ms 44452 KB Output is correct
13 Correct 39 ms 6484 KB Output is correct
14 Correct 24 ms 3584 KB Output is correct
15 Correct 285 ms 45524 KB Output is correct
16 Correct 324 ms 45552 KB Output is correct
17 Correct 290 ms 45552 KB Output is correct
18 Correct 33 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 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 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2788 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 148 ms 39764 KB Output is correct
18 Correct 157 ms 39764 KB Output is correct
19 Correct 160 ms 39892 KB Output is correct
20 Correct 171 ms 39760 KB Output is correct
21 Correct 175 ms 39016 KB Output is correct
22 Correct 167 ms 38544 KB Output is correct
23 Correct 143 ms 38468 KB Output is correct
24 Correct 174 ms 38488 KB Output is correct
25 Correct 183 ms 38500 KB Output is correct
26 Correct 183 ms 38368 KB Output is correct
27 Correct 155 ms 38452 KB Output is correct
28 Correct 153 ms 38484 KB Output is correct
29 Correct 162 ms 38496 KB Output is correct
30 Correct 154 ms 38360 KB Output is correct
31 Correct 163 ms 38468 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 2 ms 2396 KB Output is correct
36 Correct 0 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 443 ms 42084 KB Output is correct
39 Correct 450 ms 44612 KB Output is correct
40 Correct 408 ms 44924 KB Output is correct
41 Correct 437 ms 44636 KB Output is correct
42 Correct 457 ms 44508 KB Output is correct
43 Correct 398 ms 44452 KB Output is correct
44 Correct 39 ms 6484 KB Output is correct
45 Correct 24 ms 3584 KB Output is correct
46 Correct 285 ms 45524 KB Output is correct
47 Correct 324 ms 45552 KB Output is correct
48 Correct 290 ms 45552 KB Output is correct
49 Correct 33 ms 6236 KB Output is correct
50 Incorrect 174 ms 43092 KB Output isn't correct
51 Halted 0 ms 0 KB -