답안 #924597

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924597 2024-02-09T08:52:46 Z qwe1rt1yuiop1 Collecting Stamps 3 (JOI20_ho_t3) C++14
15 / 100
2000 ms 526020 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
using pii = pair<int, int>;

int n, l;

vector<int> x, t;

int dis(int a, int b)
{
    assert(0 <= a && 0 <= b && a <= n && b <= n);
    return min(min(abs(x[a] - x[b]), x[a] + l - x[b]), l - x[a] + x[b]);
}

void solve()
{
    cin >> n >> l;
    x.assign(n + 1, 0), t.assign(n + 1, -1);
    for (int i = 1; i <= n; ++i)
        cin >> x[i];
    for (int i = 1; i <= n; ++i)
        cin >> t[i];

    vector<vector<vector<vector<int>>>> dp(n + 1, vector<vector<vector<int>>>(n + 1, vector<vector<int>>(n + 1, vector<int>(2, LONG_LONG_MAX))));

    priority_queue<array<int, 5>> pq;
    array<int, 5> tmp;
    dp[0][0][0][0] = dp[0][0][0][1] = 0;
    pq.emplace(tmp = {0, 0, 0, 0, 0});
    pq.emplace(tmp = {0, 0, 0, 0, 1});
    while (!pq.empty())
    {
        tmp = pq.top();
        pq.pop();
        int i = tmp[1], j = tmp[2], k = tmp[3], ll = tmp[4], d = -tmp[0];
        if (d != dp[i][j][k][ll])
            continue;
        // cout << d << ' ';

        if (ll == 0)
        {
            if ((j + n) % (n + 1) != k)
            {
                if (dp[i][(j + n) % (n + 1)][k][0] > dp[i][j][k][0] + dis(j, (j + n) % (n + 1)))
                {
                    dp[i][(j + n) % (n + 1)][k][0] = dp[i][j][k][0] + dis(j, (j + n) % (n + 1));
                    pq.emplace(tmp = {-dp[i][(j + n) % (n + 1)][k][0], i, (j + n) % (n + 1), k, 0});
                }
                if (dp[i][j][k][0] + dis(j, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
                {
                    if (dp[i + 1][(j + n) % (n + 1)][k][0] > dp[i][j][k][0] + dis(j, (j + n) % (n + 1)))
                    {
                        dp[i + 1][(j + n) % (n + 1)][k][0] = dp[i][j][k][0] + dis(j, (j + n) % (n + 1));
                        pq.emplace(tmp = {-dp[i + 1][(j + n) % (n + 1)][k][0], i + 1, (j + n) % (n + 1), k, 0});
                    }
                }
            }
            if (j != (k + 1) % (n + 1))
            {
                if (dp[i][j][(k + 1) % (n + 1)][1] > dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)))
                {
                    dp[i][j][(k + 1) % (n + 1)][1] = dp[i][j][k][0] + dis(j, (k + 1) % (n + 1));
                    pq.emplace(tmp = {-dp[i][j][(k + 1) % (n + 1)][1], i, j, (k + 1) % (n + 1), 1});
                }
                if (dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
                {
                    if (dp[i + 1][j][(k + 1) % (n + 1)][1] > dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)))
                    {
                        dp[i + 1][j][(k + 1) % (n + 1)][1] = dp[i][j][k][0] + dis(j, (k + 1) % (n + 1));
                        pq.emplace(tmp = {-dp[i + 1][j][(k + 1) % (n + 1)][1], i + 1, j, (k + 1) % (n + 1), 1});
                    }
                }
            }
        }
        else
        {
            if ((j + n) % (n + 1) != k)
            {
                if (dp[i][(j + n) % (n + 1)][k][0] > dp[i][j][k][1] + dis(k, (j + n) % (n + 1)))
                {
                    dp[i][(j + n) % (n + 1)][k][0] = dp[i][j][k][1] + dis(k, (j + n) % (n + 1));
                    pq.emplace(tmp = {-dp[i][(j + n) % (n + 1)][k][0], i, (j + n) % (n + 1), k, 0});
                }
                if (dp[i][j][k][1] + dis(k, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
                {
                    if (dp[i + 1][(j + n) % (n + 1)][k][0] > dp[i][j][k][1] + dis(k, (j + n) % (n + 1)))
                    {
                        dp[i + 1][(j + n) % (n + 1)][k][0] = dp[i][j][k][1] + dis(k, (j + n) % (n + 1));
                        pq.emplace(tmp = {-dp[i + 1][(j + n) % (n + 1)][k][0], i + 1, (j + n) % (n + 1), k, 0});
                    }
                }
            }
            if (j != (k + 1) % (n + 1))
            {
                if (dp[i][j][(k + 1) % (n + 1)][1] > dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)))
                {
                    dp[i][j][(k + 1) % (n + 1)][1] = dp[i][j][k][1] + dis(k, (k + 1) % (n + 1));
                    pq.emplace(tmp = {-dp[i][j][(k + 1) % (n + 1)][1], i, j, (k + 1) % (n + 1), 1});
                }
                if (dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
                {
                    if (dp[i + 1][j][(k + 1) % (n + 1)][1] > dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)))
                    {
                        dp[i + 1][j][(k + 1) % (n + 1)][1] = dp[i][j][k][1] + dis(k, (k + 1) % (n + 1));
                        pq.emplace(tmp = {-dp[i + 1][j][(k + 1) % (n + 1)][1], i + 1, j, (k + 1) % (n + 1), 1});
                    }
                }
            }
        }
    }
    /*
    for (int i = 0; i < n; ++i)
        for (int j = 0; j <= n; ++j)
            for (int k = 0; k <= n; ++k)
            {
                if (dp[i][j][k][0] != LONG_LONG_MAX)
                {
                    if ((j + n) % (n + 1) != k)
                    {
                        dp[i][(j + n) % (n + 1)][k][0] = min(dp[i][(j + n) % (n + 1)][k][0], dp[i][j][k][0] + dis(j, (j + n) % (n + 1)));
                        if (dp[i][j][k][0] + dis(j, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
                            dp[i + 1][(j + n) % (n + 1)][k][0] = min(dp[i + 1][(j + n) % (n + 1)][k][0], dp[i][j][k][0] + dis(j, (j + n) % (n + 1)));
                    }
                    if (j != (k + 1) % (n + 1))
                    {
                        dp[i][j][(k + 1) % (n + 1)][1] = min(dp[i][j][(k + 1) % (n + 1)][1], dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)));
                        if (dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
                            dp[i + 1][j][(k + 1) % (n + 1)][1] = min(dp[i + 1][j][(k + 1) % (n + 1)][1], dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)));
                    }
                }
                if (dp[i][j][k][1] != LONG_LONG_MAX)
                {
                    if ((j + n) % (n + 1) != k)
                    {
                        dp[i][(j + n) % (n + 1)][k][0] = min(dp[i][(j + n) % (n + 1)][k][0], dp[i][j][k][1] + dis(k, (j + n) % (n + 1)));
                        if (dp[i][j][k][1] + dis(k, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
                            dp[i + 1][(j + n) % (n + 1)][k][0] = min(dp[i + 1][(j + n) % (n + 1)][k][0], dp[i][j][k][1] + dis(k, (j + n) % (n + 1)));
                    }
                    if (j != (k + 1) % (n + 1))
                    {
                        dp[i][j][(k + 1) % (n + 1)][1] = min(dp[i][j][(k + 1) % (n + 1)][1], dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)));
                        if (dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
                            dp[i + 1][j][(k + 1) % (n + 1)][1] = min(dp[i + 1][j][(k + 1) % (n + 1)][1], dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)));
                    }
                }
            }
*/
    int ans = 0;
    for (int i = 0; i <= n; ++i)
        for (int j = 0; j <= n; ++j)
            for (int k = 0; k <= n; ++k)
                for (int l = 0; l < 2; ++l)
                    if (dp[i][j][k][l] != LONG_LONG_MAX)
                        ans = max(ans, i);
    cout << ans << '\n';
}

/*
6 25
3 4 7 17 21 23
11 7 17 10 8 10

5 20
4 5 8 13 17
18 23 15 7 10

4 19
3 7 12 14
2 0 5 4

10 87
9 23 33 38 42 44 45 62 67 78
15 91 7 27 31 53 12 91 89 46

 */

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1501 ms 365624 KB Output is correct
19 Correct 714 ms 175028 KB Output is correct
20 Correct 231 ms 65924 KB Output is correct
21 Correct 610 ms 160908 KB Output is correct
22 Correct 1016 ms 228196 KB Output is correct
23 Correct 212 ms 52608 KB Output is correct
24 Correct 60 ms 33560 KB Output is correct
25 Correct 218 ms 51584 KB Output is correct
26 Correct 22 ms 11448 KB Output is correct
27 Correct 227 ms 55192 KB Output is correct
28 Correct 62 ms 30224 KB Output is correct
29 Correct 186 ms 55904 KB Output is correct
30 Correct 62 ms 35856 KB Output is correct
31 Correct 163 ms 51020 KB Output is correct
32 Correct 50 ms 18548 KB Output is correct
33 Correct 167 ms 51176 KB Output is correct
34 Correct 18 ms 10424 KB Output is correct
35 Correct 85 ms 45064 KB Output is correct
36 Correct 26 ms 15032 KB Output is correct
37 Correct 97 ms 49344 KB Output is correct
38 Correct 64 ms 22256 KB Output is correct
39 Correct 105 ms 52496 KB Output is correct
40 Correct 42 ms 24436 KB Output is correct
41 Execution timed out 2119 ms 526020 KB Time limit exceeded
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1501 ms 365624 KB Output is correct
37 Correct 714 ms 175028 KB Output is correct
38 Correct 231 ms 65924 KB Output is correct
39 Correct 610 ms 160908 KB Output is correct
40 Correct 1016 ms 228196 KB Output is correct
41 Correct 212 ms 52608 KB Output is correct
42 Correct 60 ms 33560 KB Output is correct
43 Correct 218 ms 51584 KB Output is correct
44 Correct 22 ms 11448 KB Output is correct
45 Correct 227 ms 55192 KB Output is correct
46 Correct 62 ms 30224 KB Output is correct
47 Correct 186 ms 55904 KB Output is correct
48 Correct 62 ms 35856 KB Output is correct
49 Correct 163 ms 51020 KB Output is correct
50 Correct 50 ms 18548 KB Output is correct
51 Correct 167 ms 51176 KB Output is correct
52 Correct 18 ms 10424 KB Output is correct
53 Correct 85 ms 45064 KB Output is correct
54 Correct 26 ms 15032 KB Output is correct
55 Correct 97 ms 49344 KB Output is correct
56 Correct 64 ms 22256 KB Output is correct
57 Correct 105 ms 52496 KB Output is correct
58 Correct 42 ms 24436 KB Output is correct
59 Execution timed out 2119 ms 526020 KB Time limit exceeded
60 Halted 0 ms 0 KB -