답안 #689591

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
689591 2023-01-28T19:25:22 Z pls33 Mecho (IOI09_mecho) C++17
100 / 100
350 ms 13940 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma region dalykai
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;
#pragma endregion

using grid_t = vector<string>;

const vi32 dx = {1, -1, 0, 0};
const vi32 dy = {0, 0, 1, -1};

struct frac_t
{
    int64_t enumer, denomin;

    frac_t(int64_t e = 0, int64_t d = 1)
    {
        enumer = e;
        denomin = d;
    }

    frac_t operator+(frac_t rhs)
    {
        int64_t d = lcm(denomin, rhs.denomin);
        int64_t e = enumer * (d / denomin) + rhs.enumer * (d / rhs.denomin);

        return {e, d};
    }

    bool operator<(frac_t rhs)
    {
        int64_t d = lcm(denomin, rhs.denomin);
        return enumer * (d / denomin) < rhs.enumer * (d / rhs.denomin);
    }

    bool operator>=(frac_t rhs)
    {
        int64_t d = lcm(denomin, rhs.denomin);
        return enumer * (d / denomin) >= rhs.enumer * (d / rhs.denomin);
    }

    bool operator<(int64_t rhs)
    {
        return *this < frac_t(rhs, 1);
    }

    bool operator>=(int64_t rhs)
    {
        return *this >= frac_t(rhs, 1);
    }

    double approx()
    {
        return (double)enumer / (double)denomin;
    }
};

using bear_t = vector<vector<frac_t>>;

bool inside(p32 pos, int n)
{
    return pos.first >= 0 && pos.first < n &&
           pos.second >= 0 && pos.second < n;
}

void get_time(vp32 &hive, vvi32 &reach_time, grid_t &grid)
{
    queue<p32> q;
    for (auto &h : hive)
    {
        q.push(h);
        reach_time[h.first][h.second] = 0;
    }

    while (!q.empty())
    {
        p32 cur = q.front();
        q.pop();

        for (int i = 0; i < (int)dx.size(); i++)
        {
            p32 next = {cur.first + dx[i], cur.second + dy[i]};

            if (inside(next, (int)grid.size()) && grid[next.first][next.second] == 'M' &&
                reach_time[next.first][next.second] == INT_MAX)
            {
                reach_time[next.first][next.second] = reach_time[cur.first][cur.second] + 1;
            }

            if (!inside(next, (int)grid.size()) ||
                grid[next.first][next.second] != 'G' ||
                reach_time[next.first][next.second] != INT_MAX)
            {
                continue;
            }

            reach_time[next.first][next.second] = reach_time[cur.first][cur.second] + 1;
            q.push(next);
        }
    }
}

bool can_reach(p32 start, p32 end, int time, frac_t speed,
               vvi32 &reach_time, bear_t &bear_time, grid_t &grid)
{
    if (time >= reach_time[start.first][start.second])
    {
        return false;
    }
    bear_time[start.first][start.second] = frac_t(time, 1);
    queue<p32> q;
    q.push(start);

    while (!q.empty())
    {
        p32 cur = q.front();
        q.pop();

        for (int i = 0; i < (int)dx.size(); i++)
        {
            p32 next = {cur.first + dx[i], cur.second + dy[i]};
            frac_t cur_time = bear_time[cur.first][cur.second] + speed;

            if (!inside(next, (int)grid.size()) ||
                (grid[next.first][next.second] != 'G' &&
                 grid[next.first][next.second] != 'D') ||
                bear_time[next.first][next.second].enumer != -1 ||
                cur_time >= reach_time[next.first][next.second])
            {
                continue;
            }

            bear_time[next.first][next.second] = cur_time;

            if (next == end)
            {
                return true;
            }

            q.push(next);
        }
    }

    return false;
}

bool test_reach(p32 start, p32 end, int time, int s,
                vvi32 &reach_time, bear_t &bear_time, grid_t &grid)
{
    frac_t speed(1, s);
    bool result = can_reach(start, end, time, speed, reach_time, bear_time, grid);

    for (auto &row : bear_time)
    {
        for (auto &b : row)
        {
            b = frac_t(-1, 1);
        }
    }

    return result;
}

int main()
{
#ifndef _AAAAAAAAA
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#else
    freopen("mecho.in", "r", stdin);
#ifndef __linux__
    atexit([]()
           {
        freopen("con", "r", stdin);
        system("pause"); });
#endif
#endif

    int n, s;
    cin >> n >> s;

    grid_t grid(n);
    vp32 hive;
    p32 start, end;
    {
        int i = 0;
        for (auto &row : grid)
        {
            cin >> row;
            for (int j = 0; j < (int)row.size(); j++)
            {
                switch (row[j])
                {
                case 'M':
                {
                    start = {i, j};
                    break;
                }
                case 'D':
                {
                    end = {i, j};
                    break;
                }
                case 'H':
                {
                    hive.emplace_back(i, j);
                    break;
                }
                }
            }

            i++;
        }
    }

    vvi32 reach_time(n, vi32(n, INT_MAX));
    bear_t bear_time(n, vector<frac_t>(n, frac_t(-1, -1)));
    get_time(hive, reach_time, grid);

    int result = 0;
    for (int bit = 29; bit >= 0; bit--)
    {
        int cur = result + (1 << bit);
        if (cur > n * n || !test_reach(start, end, cur, s, reach_time, bear_time, grid))
        {
            continue;
        }

        result = cur;
    }

    if (!result)
    {
        bool cant = !test_reach(start, end, 0, s, reach_time, bear_time, grid);
        result = cant ? -1 : result;
    }
    cout << result << '\n';

    return 0;
}

Compilation message

mecho.cpp:8: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
    8 | #pragma region dalykai
      | 
mecho.cpp:31: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
   31 | #pragma endregion
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 234 ms 13720 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 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 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 412 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 4 ms 2772 KB Output is correct
34 Correct 5 ms 2776 KB Output is correct
35 Correct 57 ms 2868 KB Output is correct
36 Correct 6 ms 3676 KB Output is correct
37 Correct 9 ms 3540 KB Output is correct
38 Correct 84 ms 3644 KB Output is correct
39 Correct 8 ms 4436 KB Output is correct
40 Correct 9 ms 4436 KB Output is correct
41 Correct 98 ms 4436 KB Output is correct
42 Correct 12 ms 5460 KB Output is correct
43 Correct 11 ms 5460 KB Output is correct
44 Correct 119 ms 5520 KB Output is correct
45 Correct 12 ms 6612 KB Output is correct
46 Correct 12 ms 6612 KB Output is correct
47 Correct 158 ms 6612 KB Output is correct
48 Correct 14 ms 7764 KB Output is correct
49 Correct 15 ms 7764 KB Output is correct
50 Correct 199 ms 7808 KB Output is correct
51 Correct 19 ms 9044 KB Output is correct
52 Correct 17 ms 9044 KB Output is correct
53 Correct 230 ms 9092 KB Output is correct
54 Correct 21 ms 10516 KB Output is correct
55 Correct 26 ms 10452 KB Output is correct
56 Correct 275 ms 10492 KB Output is correct
57 Correct 28 ms 11988 KB Output is correct
58 Correct 29 ms 11988 KB Output is correct
59 Correct 323 ms 11988 KB Output is correct
60 Correct 36 ms 13524 KB Output is correct
61 Correct 37 ms 13524 KB Output is correct
62 Correct 350 ms 13580 KB Output is correct
63 Correct 207 ms 13524 KB Output is correct
64 Correct 289 ms 13600 KB Output is correct
65 Correct 305 ms 13524 KB Output is correct
66 Correct 239 ms 13600 KB Output is correct
67 Correct 245 ms 13600 KB Output is correct
68 Correct 102 ms 13620 KB Output is correct
69 Correct 76 ms 13524 KB Output is correct
70 Correct 67 ms 13536 KB Output is correct
71 Correct 71 ms 13616 KB Output is correct
72 Correct 52 ms 13524 KB Output is correct
73 Correct 64 ms 13780 KB Output is correct
74 Correct 157 ms 13872 KB Output is correct
75 Correct 172 ms 13888 KB Output is correct
76 Correct 158 ms 13908 KB Output is correct
77 Correct 169 ms 13888 KB Output is correct
78 Correct 224 ms 13856 KB Output is correct
79 Correct 130 ms 13852 KB Output is correct
80 Correct 149 ms 13844 KB Output is correct
81 Correct 182 ms 13856 KB Output is correct
82 Correct 158 ms 13852 KB Output is correct
83 Correct 227 ms 13768 KB Output is correct
84 Correct 188 ms 13940 KB Output is correct
85 Correct 187 ms 13780 KB Output is correct
86 Correct 201 ms 13816 KB Output is correct
87 Correct 193 ms 13808 KB Output is correct
88 Correct 237 ms 13652 KB Output is correct
89 Correct 214 ms 13756 KB Output is correct
90 Correct 257 ms 13752 KB Output is correct
91 Correct 216 ms 13756 KB Output is correct
92 Correct 213 ms 13752 KB Output is correct