답안 #801498

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
801498 2023-08-02T06:21:29 Z resting Sky Walking (IOI19_walk) C++17
100 / 100
3081 ms 144520 KB
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int inf = 1e9;

long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
    int n = x.size(), m = l.size();
    vector<pair<int, pair<int, int>>> a, b, c;
    for (int i = 0; i < m; i++) a.push_back({ y[i], {l[i], r[i]} });
    sort(a.begin(), a.end());
    vector<pair<int, int>> bld;
    for (int i = 0; i < n; i++) bld.push_back({ h[i], i });
    bld.push_back({ 0, -1 }); // just for the sake of it
    sort(bld.begin(), bld.end());
    {
        int rght = inf;
        for (int i = n - 1; i >= 0; i--) {
            while (a.size() && a.back().first > bld[i].first) {
                if (rght != inf && a.back().second.first < rght && a.back().second.second > rght) {
                    b.push_back({ a.back().first, {a.back().second.first, rght } });
                    b.push_back({ a.back().first, {rght, a.back().second.second } });
                }
                else b.push_back(a.back());
                a.pop_back();
            }
            if (bld[i].second >= s)
                rght = min(rght, bld[i].second);
        }
        swap(a, b); reverse(a.begin(), a.end());
    }
    {
        int lft = -inf;
        for (int i = n - 1; i >= 0; i--) {
            while (a.size() && a.back().first > bld[i].first) {
                if (lft != -inf && a.back().second.first < lft && a.back().second.second > lft) {
                    b.push_back({ a.back().first, {a.back().second.first, lft } });
                    b.push_back({ a.back().first, {lft, a.back().second.second } });
                }
                else b.push_back(a.back());
                a.pop_back();
            }
            if (bld[i].second <= s)
                lft = max(lft, bld[i].second);
        }
        swap(a, b); reverse(a.begin(), a.end());
    }
    {
        int rght = inf;
        for (int i = n - 1; i >= 0; i--) {
            while (a.size() && a.back().first > bld[i].first) {
                if (rght != inf && a.back().second.first < rght && a.back().second.second > rght) {
                    b.push_back({ a.back().first, {a.back().second.first, rght } });
                    b.push_back({ a.back().first, {rght, a.back().second.second } });
                }
                else b.push_back(a.back());
                a.pop_back();
            }
            if (bld[i].second >= g)
                rght = min(rght, bld[i].second);
        }
        swap(a, b); reverse(a.begin(), a.end());
    }
    {
        int lft = -inf;
        for (int i = n - 1; i >= 0; i--) {
            while (a.size() && a.back().first > bld[i].first) {
                if (lft != -inf && a.back().second.first < lft && a.back().second.second > lft) {
                    b.push_back({ a.back().first, {a.back().second.first, lft } });
                    b.push_back({ a.back().first, {lft, a.back().second.second } });
                }
                else b.push_back(a.back());
                a.pop_back();
            }
            if (bld[i].second <= g)
                lft = max(lft, bld[i].second);
        }
        swap(a, b); reverse(a.begin(), a.end());
    }
    reverse(a.begin(), a.end());
    set<pair<int, int>> pts; // set of points
    map<pair<int, int>, vector<pair<int, int>>> adj;
    auto edge = [&](pair<int, int> a, pair<int, int> b) {
        adj[a].push_back(b); adj[b].push_back(a);
    };
    auto dist = [&](pair<int, int> a, pair<int, int> b) -> ll {
        return (ll)(abs(x[a.first] - x[b.first]) + abs(a.second - b.second));
    };
    for (auto& x : a) {

        //cout << x.first << "," << x.second.first << "," << x.second.second << endl;
        int prev = x.second.first;
        auto pt = pts.lower_bound({ x.second.first, 0 });
        while (pt != pts.end() && pt->first <= x.second.second) {
            edge({ pt->first, x.first }, { prev, x.first });
            edge({ pt->first, x.first }, { pt->first, pt->second });
            prev = pt->first;
            pts.erase(pt++);
        }
        edge({ x.second.second, x.first }, { prev, x.first });
        pts.insert({ x.second.first, x.first });
        pts.insert({ x.second.second, x.first });
    }
    for (auto& x : pts) {
        edge({ x.first, x.second }, { x.first, 0 });
    }

    map<pair<int, int>, ll> dst;
    set<pair<ll, pair<int, int>>> q;
    q.insert({ 1, {s, 0} });
    while (!q.empty()) {
        auto t = *q.begin();
        q.erase(q.begin());
        if (dst[t.second]) continue;
        // cout << t.first << "," << t.second.first << "," << t.second.second << endl;
        dst[t.second] = t.first;
        for (auto& x : adj[t.second]) {
            //cout << "bruh" << endl;
            q.insert({ dist(t.second, x) + t.first,  x });
        }
    }

    //construct & run dijkstra
    return dst[{g, 0}] - 1;
}

long long min_distance_stress(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {  // basically dijkstra
    int n = x.size(), m = l.size();
    map<pair<int, int>, ll> dist;
    set<pair<ll, pair<int, int>>> ss;
    ss.insert({ 1, {s, 0} });
    while (ss.size()) {
        auto t = *ss.begin();
        ss.erase(ss.begin());
        int tim = t.first, xx = t.second.first, yy = t.second.second;
        if (dist[t.second]) continue;
        //cout << xx << "," << yy << "," << tim << endl;
        dist[t.second] = t.first;
        for (int i = 0; i < m; i++) {
            for (int x2 = 0; x2 < n; x2++) {
                if (y[i] == yy && l[i] <= x2 && r[i] >= x2 && l[i] <= xx && r[i] >= xx && h[x2] >= y[i]) ss.insert({ tim + abs(x[x2] - x[xx]), {x2, yy} });
            }
            if (l[i] <= xx && r[i] >= xx && h[xx] >= y[i]) ss.insert({ tim + abs(y[i] - yy), {xx, y[i]} });
            ss.insert({ tim + yy, {xx, 0} });
        }
    }
    return dist[{g, 0}] - 1;
}

// int main() {
//     cout << min_distance_stress({ 0, 3, 5, 7, 10, 12, 14 },
//         { 8, 7, 9, 7, 6, 6, 9 },
//         { 0, 0, 0, 2, 2, 3, 4 },
//         { 1, 2, 6, 3, 6, 4, 6 },
//         { 1, 6, 8, 1, 7, 2, 5 },
//         1, 5) << endl; 
//     cout << min_distance({ 0, 3, 5, 7, 10, 12, 14 },
//         { 8, 7, 9, 7, 6, 6, 9 },
//         { 0, 0, 0, 2, 2, 3, 4 },
//         { 1, 2, 6, 3, 6, 4, 6 },
//         { 1, 6, 8, 1, 7, 2, 5 },
//         1, 5) << endl;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1217 ms 70064 KB Output is correct
4 Correct 796 ms 78524 KB Output is correct
5 Correct 368 ms 43868 KB Output is correct
6 Correct 360 ms 44204 KB Output is correct
7 Correct 354 ms 44196 KB Output is correct
8 Correct 1105 ms 72560 KB Output is correct
9 Correct 555 ms 57444 KB Output is correct
10 Correct 934 ms 78480 KB Output is correct
11 Correct 780 ms 67144 KB Output is correct
12 Correct 569 ms 74012 KB Output is correct
13 Correct 848 ms 80144 KB Output is correct
14 Correct 356 ms 50228 KB Output is correct
15 Correct 465 ms 73288 KB Output is correct
16 Correct 400 ms 69996 KB Output is correct
17 Correct 431 ms 67040 KB Output is correct
18 Correct 470 ms 59940 KB Output is correct
19 Correct 15 ms 3768 KB Output is correct
20 Correct 206 ms 35616 KB Output is correct
21 Correct 361 ms 67488 KB Output is correct
22 Correct 496 ms 73092 KB Output is correct
23 Correct 592 ms 76808 KB Output is correct
24 Correct 436 ms 71076 KB Output is correct
25 Correct 433 ms 66984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 18664 KB Output is correct
2 Correct 1213 ms 75892 KB Output is correct
3 Correct 1236 ms 77252 KB Output is correct
4 Correct 1414 ms 79452 KB Output is correct
5 Correct 1711 ms 82868 KB Output is correct
6 Correct 1601 ms 83928 KB Output is correct
7 Correct 510 ms 41652 KB Output is correct
8 Correct 610 ms 69948 KB Output is correct
9 Correct 1618 ms 88444 KB Output is correct
10 Correct 531 ms 67928 KB Output is correct
11 Correct 13 ms 1676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 18664 KB Output is correct
2 Correct 1213 ms 75892 KB Output is correct
3 Correct 1236 ms 77252 KB Output is correct
4 Correct 1414 ms 79452 KB Output is correct
5 Correct 1711 ms 82868 KB Output is correct
6 Correct 1601 ms 83928 KB Output is correct
7 Correct 510 ms 41652 KB Output is correct
8 Correct 610 ms 69948 KB Output is correct
9 Correct 1618 ms 88444 KB Output is correct
10 Correct 531 ms 67928 KB Output is correct
11 Correct 13 ms 1676 KB Output is correct
12 Correct 1380 ms 78984 KB Output is correct
13 Correct 549 ms 58300 KB Output is correct
14 Correct 1839 ms 86108 KB Output is correct
15 Correct 905 ms 77212 KB Output is correct
16 Correct 928 ms 74704 KB Output is correct
17 Correct 1004 ms 83752 KB Output is correct
18 Correct 966 ms 77148 KB Output is correct
19 Correct 980 ms 74508 KB Output is correct
20 Correct 577 ms 43636 KB Output is correct
21 Correct 39 ms 5312 KB Output is correct
22 Correct 440 ms 52036 KB Output is correct
23 Correct 374 ms 49416 KB Output is correct
24 Correct 335 ms 48332 KB Output is correct
25 Correct 387 ms 47208 KB Output is correct
26 Correct 305 ms 50152 KB Output is correct
27 Correct 1723 ms 84732 KB Output is correct
28 Correct 510 ms 58188 KB Output is correct
29 Correct 1528 ms 80380 KB Output is correct
30 Correct 515 ms 44636 KB Output is correct
31 Correct 1370 ms 78048 KB Output is correct
32 Correct 393 ms 65696 KB Output is correct
33 Correct 464 ms 68512 KB Output is correct
34 Correct 513 ms 70880 KB Output is correct
35 Correct 495 ms 72488 KB Output is correct
36 Correct 470 ms 68972 KB Output is correct
37 Correct 382 ms 67456 KB Output is correct
38 Correct 525 ms 73184 KB Output is correct
39 Correct 599 ms 76808 KB Output is correct
40 Correct 404 ms 71100 KB Output is correct
41 Correct 423 ms 66972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1217 ms 70064 KB Output is correct
21 Correct 796 ms 78524 KB Output is correct
22 Correct 368 ms 43868 KB Output is correct
23 Correct 360 ms 44204 KB Output is correct
24 Correct 354 ms 44196 KB Output is correct
25 Correct 1105 ms 72560 KB Output is correct
26 Correct 555 ms 57444 KB Output is correct
27 Correct 934 ms 78480 KB Output is correct
28 Correct 780 ms 67144 KB Output is correct
29 Correct 569 ms 74012 KB Output is correct
30 Correct 848 ms 80144 KB Output is correct
31 Correct 356 ms 50228 KB Output is correct
32 Correct 465 ms 73288 KB Output is correct
33 Correct 400 ms 69996 KB Output is correct
34 Correct 431 ms 67040 KB Output is correct
35 Correct 470 ms 59940 KB Output is correct
36 Correct 15 ms 3768 KB Output is correct
37 Correct 206 ms 35616 KB Output is correct
38 Correct 361 ms 67488 KB Output is correct
39 Correct 496 ms 73092 KB Output is correct
40 Correct 592 ms 76808 KB Output is correct
41 Correct 436 ms 71076 KB Output is correct
42 Correct 433 ms 66984 KB Output is correct
43 Correct 119 ms 18664 KB Output is correct
44 Correct 1213 ms 75892 KB Output is correct
45 Correct 1236 ms 77252 KB Output is correct
46 Correct 1414 ms 79452 KB Output is correct
47 Correct 1711 ms 82868 KB Output is correct
48 Correct 1601 ms 83928 KB Output is correct
49 Correct 510 ms 41652 KB Output is correct
50 Correct 610 ms 69948 KB Output is correct
51 Correct 1618 ms 88444 KB Output is correct
52 Correct 531 ms 67928 KB Output is correct
53 Correct 13 ms 1676 KB Output is correct
54 Correct 1380 ms 78984 KB Output is correct
55 Correct 549 ms 58300 KB Output is correct
56 Correct 1839 ms 86108 KB Output is correct
57 Correct 905 ms 77212 KB Output is correct
58 Correct 928 ms 74704 KB Output is correct
59 Correct 1004 ms 83752 KB Output is correct
60 Correct 966 ms 77148 KB Output is correct
61 Correct 980 ms 74508 KB Output is correct
62 Correct 577 ms 43636 KB Output is correct
63 Correct 39 ms 5312 KB Output is correct
64 Correct 440 ms 52036 KB Output is correct
65 Correct 374 ms 49416 KB Output is correct
66 Correct 335 ms 48332 KB Output is correct
67 Correct 387 ms 47208 KB Output is correct
68 Correct 305 ms 50152 KB Output is correct
69 Correct 1723 ms 84732 KB Output is correct
70 Correct 510 ms 58188 KB Output is correct
71 Correct 1528 ms 80380 KB Output is correct
72 Correct 515 ms 44636 KB Output is correct
73 Correct 1370 ms 78048 KB Output is correct
74 Correct 393 ms 65696 KB Output is correct
75 Correct 464 ms 68512 KB Output is correct
76 Correct 513 ms 70880 KB Output is correct
77 Correct 495 ms 72488 KB Output is correct
78 Correct 470 ms 68972 KB Output is correct
79 Correct 382 ms 67456 KB Output is correct
80 Correct 525 ms 73184 KB Output is correct
81 Correct 599 ms 76808 KB Output is correct
82 Correct 404 ms 71100 KB Output is correct
83 Correct 423 ms 66972 KB Output is correct
84 Correct 100 ms 16032 KB Output is correct
85 Correct 1413 ms 83196 KB Output is correct
86 Correct 2275 ms 112560 KB Output is correct
87 Correct 59 ms 11644 KB Output is correct
88 Correct 72 ms 11320 KB Output is correct
89 Correct 66 ms 11868 KB Output is correct
90 Correct 32 ms 5564 KB Output is correct
91 Correct 2 ms 340 KB Output is correct
92 Correct 24 ms 3408 KB Output is correct
93 Correct 439 ms 31668 KB Output is correct
94 Correct 33 ms 5668 KB Output is correct
95 Correct 488 ms 58196 KB Output is correct
96 Correct 399 ms 49832 KB Output is correct
97 Correct 347 ms 48284 KB Output is correct
98 Correct 345 ms 47332 KB Output is correct
99 Correct 3081 ms 144520 KB Output is correct
100 Correct 1494 ms 83800 KB Output is correct
101 Correct 1817 ms 101632 KB Output is correct
102 Correct 614 ms 44928 KB Output is correct
103 Correct 426 ms 65600 KB Output is correct
104 Correct 428 ms 68348 KB Output is correct
105 Correct 450 ms 67248 KB Output is correct
106 Correct 483 ms 68508 KB Output is correct
107 Correct 514 ms 71880 KB Output is correct
108 Correct 54 ms 6820 KB Output is correct
109 Correct 1106 ms 68632 KB Output is correct
110 Correct 563 ms 63900 KB Output is correct
111 Correct 515 ms 59520 KB Output is correct
112 Correct 484 ms 69592 KB Output is correct
113 Correct 468 ms 68748 KB Output is correct