Submission #1087414

# Submission time Handle Problem Language Result Execution time Memory
1087414 2024-09-12T16:06:13 Z pokmui9909 Cultivation (JOI17_cultivation) C++17
100 / 100
1859 ms 4856 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = 1e18;
#define x first
#define y second
ll H, W, N, F[3][305][305];
pair<ll, ll> A[305];

struct Data{
    ll l, r, c[3];
};

ll Add[605], Del[605];

ll Solve(ll len){
    for(ll i = 0; i <= 2 * N; i++){
        Add[i] = Del[i] = 0;
    }
    vector<ll> Num;
    A[N + 1].x = 1e18;
    for(ll i = 1, j = 1; j <= N;){
        if(A[i].x < A[j].x + len + 1){
            Num.push_back(A[i].x);
            i++;
        } else {
            Num.push_back(A[j].x + len + 1);
            j++;
        }
    }
    Num.erase(unique(Num.begin(), Num.end()), Num.end());
    vector<Data> V(Num.size());
    for(ll i = 1, p = 0, q = 0; i <= N; i++){
        while(p + 1 < Num.size() && !(Num[p] <= A[i].x && A[i].x < Num[p + 1])) p++;
        while(q + 1 < Num.size() && !(Num[q] <= A[i].x + len && A[i].x + len < Num[q + 1])) q++;
        Add[p]++; Del[q]++;
    }
    ll s = 1, e = 0;
    for(ll i = 0; i < V.size(); i++){
        e += Add[i];
        V[i].l = Num[i], V[i].r = (i + 1 == V.size() ? INF : Num[i + 1] - 1);
        for(ll j = 0; j < 3; j++) V[i].c[j] = F[j][s][e];
        s += Del[i];
    }
    deque<ll> dq[3];
    ll Res = 1e18;
    for(ll i = 0, j = 0; i < V.size(); i++){
        if(V[i].l + H - 1 >= V.back().l) break;
        for(ll k = 0; k < 3; k++){
            while(!dq[k].empty() && dq[k].front() < i) dq[k].pop_front();
        }
        while(j < V.size() && V[j].l <= V[i].l + H - 1){
            for(ll k = 0; k < 3; k++){
                while(!dq[k].empty() && V[dq[k].back()].c[k] <= V[j].c[k]) dq[k].pop_back();
                dq[k].push_back(j);
            }
            j++;
        }
        Res = min(Res, max(V[dq[0].front()].c[0] + V[dq[1].front()].c[1], V[dq[2].front()].c[2]));
    }
    return Res;
}

int main(){
    cin.tie(0) -> sync_with_stdio(0);

    cin >> H >> W >> N;
    vector<ll> Cand;
    for(ll i = 1; i <= N; i++){
        cin >> A[i].x >> A[i].y;
        Cand.push_back(A[i].x - 1);
        Cand.push_back(H - A[i].x);
    }
    sort(A + 1, A + N + 1);
    for(ll i = 1; i <= N; i++){
        set<ll> S; multiset<ll> Diff;
        for(ll j = i; j <= N; j++){
            auto it = S.lower_bound(A[j].y);
            if(it != S.end()){
                if(it != S.begin()){
                    ll v = *it - *prev(it) - 1;
                    Diff.erase(Diff.find(v));
                }
                Diff.insert(*it - A[j].y - 1);
            }
            if(it != S.begin()){
                Diff.insert(A[j].y - *prev(it) - 1);
            }
            S.insert(A[j].y);
            
            F[0][i][j] = *S.begin() - 1, F[1][i][j] = W - *prev(S.end());
            if(!Diff.empty()) F[2][i][j] = *prev(Diff.end());
        }
    }
    ll mn = H - A[N].x + A[1].x - 1;
    for(ll i = 2; i <= N; i++){
        mn = max(mn, A[i].x - A[i - 1].x - 1);
    }
    ll Ans = 1e18;
    for(ll i = 1; i <= N; i++){
        for(ll j = 1; j <= N; j++){
            Cand.push_back(A[j].x - A[i].x - 1);
            Cand.push_back(A[j].x - 1 + H - A[i].x);
        }
    }
    sort(Cand.begin(), Cand.end());
    Cand.erase(unique(Cand.begin(), Cand.end()), Cand.end());
    for(auto i : Cand){
        if(i < mn) continue;
        Ans = min(Ans, i + Solve(i));
    }
    cout << Ans;
}

Compilation message

cultivation.cpp: In function 'll Solve(ll)':
cultivation.cpp:35:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |         while(p + 1 < Num.size() && !(Num[p] <= A[i].x && A[i].x < Num[p + 1])) p++;
      |               ~~~~~~^~~~~~~~~~~~
cultivation.cpp:36:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         while(q + 1 < Num.size() && !(Num[q] <= A[i].x + len && A[i].x + len < Num[q + 1])) q++;
      |               ~~~~~~^~~~~~~~~~~~
cultivation.cpp:40:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(ll i = 0; i < V.size(); i++){
      |                   ~~^~~~~~~~~~
cultivation.cpp:42:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         V[i].l = Num[i], V[i].r = (i + 1 == V.size() ? INF : Num[i + 1] - 1);
      |                                    ~~~~~~^~~~~~~~~~~
cultivation.cpp:48:28: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(ll i = 0, j = 0; i < V.size(); i++){
      |                          ~~^~~~~~~~~~
cultivation.cpp:53:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         while(j < V.size() && V[j].l <= V[i].l + H - 1){
      |               ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 500 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 500 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 8 ms 2904 KB Output is correct
25 Correct 5 ms 2020 KB Output is correct
26 Correct 16 ms 4608 KB Output is correct
27 Correct 16 ms 4724 KB Output is correct
28 Correct 9 ms 3040 KB Output is correct
29 Correct 16 ms 4836 KB Output is correct
30 Correct 17 ms 4828 KB Output is correct
31 Correct 17 ms 4836 KB Output is correct
32 Correct 24 ms 4776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 500 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 8 ms 2904 KB Output is correct
25 Correct 5 ms 2020 KB Output is correct
26 Correct 16 ms 4608 KB Output is correct
27 Correct 16 ms 4724 KB Output is correct
28 Correct 9 ms 3040 KB Output is correct
29 Correct 16 ms 4836 KB Output is correct
30 Correct 17 ms 4828 KB Output is correct
31 Correct 17 ms 4836 KB Output is correct
32 Correct 24 ms 4776 KB Output is correct
33 Correct 17 ms 4748 KB Output is correct
34 Correct 13 ms 4832 KB Output is correct
35 Correct 19 ms 4840 KB Output is correct
36 Correct 18 ms 4856 KB Output is correct
37 Correct 25 ms 4696 KB Output is correct
38 Correct 18 ms 4836 KB Output is correct
39 Correct 18 ms 4840 KB Output is correct
40 Correct 18 ms 4836 KB Output is correct
41 Correct 14 ms 4648 KB Output is correct
42 Correct 18 ms 4832 KB Output is correct
43 Correct 18 ms 4832 KB Output is correct
44 Correct 18 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 49 ms 1756 KB Output is correct
22 Correct 103 ms 1504 KB Output is correct
23 Correct 47 ms 1504 KB Output is correct
24 Correct 47 ms 1504 KB Output is correct
25 Correct 112 ms 1504 KB Output is correct
26 Correct 25 ms 1504 KB Output is correct
27 Correct 72 ms 1504 KB Output is correct
28 Correct 95 ms 1504 KB Output is correct
29 Correct 60 ms 1496 KB Output is correct
30 Correct 73 ms 1500 KB Output is correct
31 Correct 70 ms 1504 KB Output is correct
32 Correct 69 ms 1500 KB Output is correct
33 Correct 47 ms 1504 KB Output is correct
34 Correct 67 ms 1504 KB Output is correct
35 Correct 69 ms 1504 KB Output is correct
36 Correct 48 ms 1504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 500 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 8 ms 2904 KB Output is correct
25 Correct 5 ms 2020 KB Output is correct
26 Correct 16 ms 4608 KB Output is correct
27 Correct 16 ms 4724 KB Output is correct
28 Correct 9 ms 3040 KB Output is correct
29 Correct 16 ms 4836 KB Output is correct
30 Correct 17 ms 4828 KB Output is correct
31 Correct 17 ms 4836 KB Output is correct
32 Correct 24 ms 4776 KB Output is correct
33 Correct 17 ms 4748 KB Output is correct
34 Correct 13 ms 4832 KB Output is correct
35 Correct 19 ms 4840 KB Output is correct
36 Correct 18 ms 4856 KB Output is correct
37 Correct 25 ms 4696 KB Output is correct
38 Correct 18 ms 4836 KB Output is correct
39 Correct 18 ms 4840 KB Output is correct
40 Correct 18 ms 4836 KB Output is correct
41 Correct 14 ms 4648 KB Output is correct
42 Correct 18 ms 4832 KB Output is correct
43 Correct 18 ms 4832 KB Output is correct
44 Correct 18 ms 4668 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 460 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 464 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 2 ms 604 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 2 ms 604 KB Output is correct
61 Correct 2 ms 604 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 2 ms 604 KB Output is correct
64 Correct 2 ms 604 KB Output is correct
65 Correct 49 ms 1756 KB Output is correct
66 Correct 103 ms 1504 KB Output is correct
67 Correct 47 ms 1504 KB Output is correct
68 Correct 47 ms 1504 KB Output is correct
69 Correct 112 ms 1504 KB Output is correct
70 Correct 25 ms 1504 KB Output is correct
71 Correct 72 ms 1504 KB Output is correct
72 Correct 95 ms 1504 KB Output is correct
73 Correct 60 ms 1496 KB Output is correct
74 Correct 73 ms 1500 KB Output is correct
75 Correct 70 ms 1504 KB Output is correct
76 Correct 69 ms 1500 KB Output is correct
77 Correct 47 ms 1504 KB Output is correct
78 Correct 67 ms 1504 KB Output is correct
79 Correct 69 ms 1504 KB Output is correct
80 Correct 48 ms 1504 KB Output is correct
81 Correct 732 ms 3040 KB Output is correct
82 Correct 726 ms 3032 KB Output is correct
83 Correct 841 ms 3300 KB Output is correct
84 Correct 1173 ms 3296 KB Output is correct
85 Correct 1816 ms 4832 KB Output is correct
86 Correct 1770 ms 4832 KB Output is correct
87 Correct 1253 ms 4832 KB Output is correct
88 Correct 1265 ms 4668 KB Output is correct
89 Correct 1348 ms 4832 KB Output is correct
90 Correct 921 ms 4580 KB Output is correct
91 Correct 1687 ms 4832 KB Output is correct
92 Correct 1859 ms 4832 KB Output is correct
93 Correct 1369 ms 4832 KB Output is correct
94 Correct 1832 ms 4836 KB Output is correct
95 Correct 1820 ms 4832 KB Output is correct
96 Correct 1809 ms 4712 KB Output is correct
97 Correct 1827 ms 4836 KB Output is correct
98 Correct 15 ms 4836 KB Output is correct