답안 #627750

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
627750 2022-08-12T21:40:43 Z tutis 메기 농장 (IOI22_fish) C++17
100 / 100
310 ms 50648 KB
#include "fish.h"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long long max_weights(int N, int MM, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
    vector<pair<int, ll>>P[N];
    vector<int>Q_[N + 4];
    vector<int>*Q = Q_ + 2;
    vector<pair<int, ll>>PP[N];
    for (int i = 0; i < MM; i++)
        PP[Y[i]].push_back({X[i], W[i]});
    for (int y = 0; y < N; y++)
    {
        for (pair<int, ll>xa : PP[y])
        {
            P[xa.first].push_back({y, xa.second});
            Q[xa.first].push_back(y);
        }
    }
    for (int i = -1; i <= N; i++)
        Q[i].push_back(N);
    for (int i = 0; i < N; i++)
    {
        for (int j = 1; j < (int)P[i].size(); j++)
            P[i][j].second += P[i][j - 1].second;
    }
    ll dp_[N + 3];
    ll* dp = dp_ + 2;
    for (int i = -2; i <= N; i++)
        dp[i] = 0;
    vector<ll> u[N];
    vector<ll> d[N];
    auto get = [&](int x, int y)->ll
    {
        if (x < 0 || x >= N)
            return 0;
        auto it = lower_bound(P[x].begin(), P[x].end(), make_pair(y + 1, -1ll));
        if (it == P[x].begin())
            return 0;
        it--;
        return it->second;
    };
    ll vv = 0;
    for (int x = 0; x < N; x++)
    {
        dp[x] = dp[x - 1];
        dp[x] = max(dp[x], vv);
        vv = 0;
        for (int y : Q[x - 1])
            u[x].push_back(0);
        for (int y : Q[x + 1])
            d[x].push_back(0);
        for (int i = 0; i < (int)Q[x - 1].size(); i++)
        {
            ll&v = u[x][i];
            int y = Q[x - 1][i];
            v = max(v, dp[x - 1]);
            v = max(v, dp[x - 2] + get(x - 1, y));
        }
        if (x != 0)
        {
            ll mx = -1e18;
            int j = 0;
            for (int i = 0; i < (int)Q[x - 1].size(); i++)
            {
                ll&v = u[x][i];
                int y = Q[x - 1][i];
                while (j < (int)Q[x - 2].size() && Q[x - 2][j] < y)
                {
                    mx = max(mx, u[x - 1][j] - get(x - 1, Q[x - 2][j]));
                    j++;
                }
                v = max(v, mx + get(x - 1, y));
            }
        }
        for (int i = 0; i < (int)Q[x - 1].size(); i++)
        {
            ll&v = u[x][i];
            int y = Q[x - 1][i];
            dp[x] = max(dp[x], v);
            vv = max(vv, v + get(x + 1, y));
        }
        for (int i = 0; i < (int)Q[x + 1].size(); i++)
        {
            ll&v = d[x][i];
            int y = Q[x + 1][i];
            if (y == N)
                v = max(v, u[x].back());
            v = max(v, dp[x - 1]);
            v = max(v, dp[x - 2] + get(x - 1, y));
        }
        if (x != 0)
        {
            int j = (int)Q[x].size() - 1;
            ll mx = -1e18;
            for (int i = (int)Q[x + 1].size() - 1; i >= 0; i--)
            {
                ll&v = d[x][i];
                int y = Q[x + 1][i];
                while (j >= 0 && Q[x][j] > y)
                {
                    mx = max(mx, d[x - 1][j] + get(x, Q[x][j]));
                    j--;
                }
                v = max(v, mx - get(x, y));
            }
        }
        for (int i = 0; i < (int)Q[x + 1].size(); i++)
        {
            ll&v = d[x][i];
            int y = Q[x + 1][i];
            dp[x] = max(dp[x], v);
            vv = max(vv, v + get(x + 1, y));
        }
    }
    return dp[N - 1];
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:51:18: warning: unused variable 'y' [-Wunused-variable]
   51 |         for (int y : Q[x - 1])
      |                  ^
fish.cpp:53:18: warning: unused variable 'y' [-Wunused-variable]
   53 |         for (int y : Q[x + 1])
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 26548 KB Output is correct
2 Correct 77 ms 30364 KB Output is correct
3 Correct 26 ms 22188 KB Output is correct
4 Correct 28 ms 22156 KB Output is correct
5 Correct 218 ms 48704 KB Output is correct
6 Correct 278 ms 50648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 117 ms 33080 KB Output is correct
3 Correct 146 ms 38152 KB Output is correct
4 Correct 66 ms 26636 KB Output is correct
5 Correct 83 ms 30328 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 29 ms 22132 KB Output is correct
11 Correct 24 ms 22156 KB Output is correct
12 Correct 71 ms 27252 KB Output is correct
13 Correct 93 ms 31228 KB Output is correct
14 Correct 70 ms 27068 KB Output is correct
15 Correct 79 ms 29492 KB Output is correct
16 Correct 73 ms 27104 KB Output is correct
17 Correct 79 ms 30016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 22204 KB Output is correct
2 Correct 24 ms 22100 KB Output is correct
3 Correct 61 ms 23708 KB Output is correct
4 Correct 51 ms 24640 KB Output is correct
5 Correct 111 ms 29244 KB Output is correct
6 Correct 100 ms 29244 KB Output is correct
7 Correct 102 ms 29240 KB Output is correct
8 Correct 102 ms 29252 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 31 ms 4428 KB Output is correct
18 Correct 29 ms 5688 KB Output is correct
19 Correct 24 ms 5372 KB Output is correct
20 Correct 23 ms 5452 KB Output is correct
21 Correct 24 ms 5460 KB Output is correct
22 Correct 57 ms 10580 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 19 ms 3028 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 5 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 31 ms 4428 KB Output is correct
18 Correct 29 ms 5688 KB Output is correct
19 Correct 24 ms 5372 KB Output is correct
20 Correct 23 ms 5452 KB Output is correct
21 Correct 24 ms 5460 KB Output is correct
22 Correct 57 ms 10580 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 19 ms 3028 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 5 ms 1108 KB Output is correct
27 Correct 3 ms 1108 KB Output is correct
28 Correct 155 ms 24436 KB Output is correct
29 Correct 205 ms 29128 KB Output is correct
30 Correct 180 ms 28120 KB Output is correct
31 Correct 184 ms 28132 KB Output is correct
32 Correct 197 ms 34528 KB Output is correct
33 Correct 188 ms 28304 KB Output is correct
34 Correct 182 ms 28336 KB Output is correct
35 Correct 70 ms 12164 KB Output is correct
36 Correct 203 ms 30784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 22204 KB Output is correct
2 Correct 24 ms 22100 KB Output is correct
3 Correct 61 ms 23708 KB Output is correct
4 Correct 51 ms 24640 KB Output is correct
5 Correct 111 ms 29244 KB Output is correct
6 Correct 100 ms 29244 KB Output is correct
7 Correct 102 ms 29240 KB Output is correct
8 Correct 102 ms 29252 KB Output is correct
9 Correct 84 ms 30784 KB Output is correct
10 Correct 75 ms 20572 KB Output is correct
11 Correct 183 ms 40968 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 30 ms 22136 KB Output is correct
19 Correct 29 ms 22156 KB Output is correct
20 Correct 26 ms 22212 KB Output is correct
21 Correct 26 ms 22120 KB Output is correct
22 Correct 119 ms 30796 KB Output is correct
23 Correct 171 ms 38252 KB Output is correct
24 Correct 168 ms 39884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 26548 KB Output is correct
2 Correct 77 ms 30364 KB Output is correct
3 Correct 26 ms 22188 KB Output is correct
4 Correct 28 ms 22156 KB Output is correct
5 Correct 218 ms 48704 KB Output is correct
6 Correct 278 ms 50648 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 117 ms 33080 KB Output is correct
9 Correct 146 ms 38152 KB Output is correct
10 Correct 66 ms 26636 KB Output is correct
11 Correct 83 ms 30328 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 29 ms 22132 KB Output is correct
17 Correct 24 ms 22156 KB Output is correct
18 Correct 71 ms 27252 KB Output is correct
19 Correct 93 ms 31228 KB Output is correct
20 Correct 70 ms 27068 KB Output is correct
21 Correct 79 ms 29492 KB Output is correct
22 Correct 73 ms 27104 KB Output is correct
23 Correct 79 ms 30016 KB Output is correct
24 Correct 25 ms 22204 KB Output is correct
25 Correct 24 ms 22100 KB Output is correct
26 Correct 61 ms 23708 KB Output is correct
27 Correct 51 ms 24640 KB Output is correct
28 Correct 111 ms 29244 KB Output is correct
29 Correct 100 ms 29244 KB Output is correct
30 Correct 102 ms 29240 KB Output is correct
31 Correct 102 ms 29252 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 576 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 2 ms 468 KB Output is correct
48 Correct 31 ms 4428 KB Output is correct
49 Correct 29 ms 5688 KB Output is correct
50 Correct 24 ms 5372 KB Output is correct
51 Correct 23 ms 5452 KB Output is correct
52 Correct 24 ms 5460 KB Output is correct
53 Correct 57 ms 10580 KB Output is correct
54 Correct 6 ms 1236 KB Output is correct
55 Correct 19 ms 3028 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 5 ms 1108 KB Output is correct
58 Correct 3 ms 1108 KB Output is correct
59 Correct 155 ms 24436 KB Output is correct
60 Correct 205 ms 29128 KB Output is correct
61 Correct 180 ms 28120 KB Output is correct
62 Correct 184 ms 28132 KB Output is correct
63 Correct 197 ms 34528 KB Output is correct
64 Correct 188 ms 28304 KB Output is correct
65 Correct 182 ms 28336 KB Output is correct
66 Correct 70 ms 12164 KB Output is correct
67 Correct 203 ms 30784 KB Output is correct
68 Correct 84 ms 30784 KB Output is correct
69 Correct 75 ms 20572 KB Output is correct
70 Correct 183 ms 40968 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 30 ms 22136 KB Output is correct
78 Correct 29 ms 22156 KB Output is correct
79 Correct 26 ms 22212 KB Output is correct
80 Correct 26 ms 22120 KB Output is correct
81 Correct 119 ms 30796 KB Output is correct
82 Correct 171 ms 38252 KB Output is correct
83 Correct 168 ms 39884 KB Output is correct
84 Correct 259 ms 45700 KB Output is correct
85 Correct 258 ms 47888 KB Output is correct
86 Correct 289 ms 46924 KB Output is correct
87 Correct 290 ms 49240 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 310 ms 49268 KB Output is correct
90 Correct 224 ms 48436 KB Output is correct
91 Correct 206 ms 48724 KB Output is correct