Submission #770434

# Submission time Handle Problem Language Result Execution time Memory
770434 2023-07-01T08:01:29 Z jlallas384 Catfish Farm (IOI22_fish) C++17
100 / 100
418 ms 78340 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;
 
long long max_weights(int n, int m, vector<int> x, vector<int> y,
                      vector<int> w) { 
    vector<vector<pair<int,int>>> g(n + 2, vector<pair<int,int>>(1, {0,0}));
    vector<vector<int>> pts(n + 2, vector<int>(1, 0));
    for(int i = 0; i < m; i++){
        g[x[i] + 1].emplace_back(y[i] + 1, w[i]);
        if(x[i] != 0) pts[x[i]].push_back(y[i] + 1);
        pts[x[i] + 2].push_back(y[i] + 1);
    }
    for(int i = 0; i <= n + 1; i++){
        sort(g[i].begin(), g[i].end());
        sort(pts[i].begin(), pts[i].end());
        pts[i].resize(unique(pts[i].begin(), pts[i].end()) - pts[i].begin());
    }
    for(int i = 1; i <= n; i++){
        if(pts[i].back() != n){
            pts[i].push_back(n);
        }
    }
    vector<vector<ll>> sums(n + 2);
    for(int i = 0; i <= n + 1; i++){
        for(auto [j, x]: g[i]){
            sums[i].push_back(x + (sums[i].empty() ? 0 : sums[i].back()));
        }
    }
    vector<vector<ll>> dpl(n + 1);
    vector<vector<ll>> dpr(n + 1);
    vector<vector<ll>> dpr2(n + 1);
    vector<vector<ll>> ans(n + 1);
    dpl[0].push_back(0);
    dpr[0].push_back(0);
    dpr2[0].push_back(0);
    ll fans = 0;
    ll pre = 0;
    for(int col = 1; col <= n; col++){
        for(int row: pts[col]){
 
            if(col >= 3){
                pre = max(dpr[col - 3].front(), pre);
                pre = max(dpl[col - 3].back(), pre);
            }
 
            auto it = prev(upper_bound(g[col].begin(), g[col].end(), make_pair(row, INF)));
            ll sumUp = sums[col].back() - sums[col][it - g[col].begin()];
            it = prev(upper_bound(g[col - 1].begin(), g[col - 1].end(), make_pair(row, INF)));
            ll removeLeft = sums[col - 1].back() - sums[col - 1][it - g[col - 1].begin()];
 
            auto it2 = prev(upper_bound(pts[col - 1].begin(), pts[col - 1].end(), row));
 
            // left to left
            if(row == 0){
                dpl[col].push_back(dpl[col - 1][it2 - pts[col - 1].begin()] + sumUp - removeLeft);
            }else{
                dpl[col].push_back(max(dpl[col].back(), dpl[col - 1][it2 - pts[col - 1].begin()] + sumUp - removeLeft));
            }
 
            //left to right
            it = prev(upper_bound(g[col + 1].begin(), g[col + 1].end(), make_pair(row, INF)));
            ll rsum = sums[col + 1][it - g[col + 1].begin()];
            dpr[col].push_back(dpl[col - 1][it2 - pts[col - 1].begin()] - removeLeft + rsum);
 
            // right to right
            it = prev(upper_bound(g[col].begin(), g[col].end(), make_pair(row, INF)));
            it2 = lower_bound(pts[col - 1].begin(), pts[col - 1].end(), row);
 
            ll ss = 0;

            if(it2 != pts[col - 1].end()){
                ss = dpr[col - 1][it2 - pts[col - 1].begin()] - sums[col][it - g[col].begin()];
                dpr[col].back() = max(dpr[col].back(), dpr[col - 1][it2 - pts[col - 1].begin()] + rsum - sums[col][it - g[col].begin()]);
            }
 
            if(row == 0) dpr2[col].push_back(dpr[col].back() - rsum);
            else dpr2[col].push_back(max(dpr2[col].back(), ss));
 
            if(col >= 2){
                // lower prev higher now 
                it2 = prev(upper_bound(pts[col - 2].begin(), pts[col - 2].end(), row));
 
                dpl[col].back() = max(dpl[col].back(), dpr2[col - 2][it2 - pts[col - 2].begin()] + sumUp + sums[col - 1].back() - removeLeft);
 
                it2 = lower_bound(pts[col - 2].begin(), pts[col - 2].end(), row);
                if(it2 != pts[col - 2].end()){
                    dpr[col].back() = max(dpr[col].back(), dpr[col - 2][it2 - pts[col - 2].begin()] + rsum);
                }
            }
 
            dpl[col].back() = max(dpl[col].back(), pre + sumUp + sums[col - 1].back() - removeLeft);
            dpr[col].back() = max(dpr[col].back(), pre + sums[col - 1].back() - removeLeft + rsum);
            fans = max(fans, dpr[col].back());
        }
        for(int i = (int) dpr[col].size() - 2; i >= 0; i--){
            dpr[col][i] = max(dpr[col][i], dpr[col][i + 1]);
        }
    }
    return fans;
}
# Verdict Execution time Memory Grader output
1 Correct 85 ms 37328 KB Output is correct
2 Correct 99 ms 42092 KB Output is correct
3 Correct 64 ms 35404 KB Output is correct
4 Correct 61 ms 35436 KB Output is correct
5 Correct 300 ms 64588 KB Output is correct
6 Correct 370 ms 71780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 188 ms 45248 KB Output is correct
3 Correct 219 ms 51640 KB Output is correct
4 Correct 104 ms 37308 KB Output is correct
5 Correct 104 ms 42168 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 55 ms 35424 KB Output is correct
11 Correct 54 ms 35440 KB Output is correct
12 Correct 115 ms 39768 KB Output is correct
13 Correct 121 ms 44976 KB Output is correct
14 Correct 102 ms 38680 KB Output is correct
15 Correct 133 ms 42868 KB Output is correct
16 Correct 111 ms 38608 KB Output is correct
17 Correct 109 ms 42768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 35484 KB Output is correct
2 Correct 57 ms 35408 KB Output is correct
3 Correct 105 ms 36000 KB Output is correct
4 Correct 78 ms 38220 KB Output is correct
5 Correct 119 ms 42532 KB Output is correct
6 Correct 122 ms 42556 KB Output is correct
7 Correct 132 ms 42548 KB Output is correct
8 Correct 157 ms 42568 KB Output is correct
# Verdict Execution time Memory 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 376 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory 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 376 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 360 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 35 ms 5004 KB Output is correct
18 Correct 34 ms 5560 KB Output is correct
19 Correct 28 ms 5576 KB Output is correct
20 Correct 23 ms 5184 KB Output is correct
21 Correct 22 ms 5188 KB Output is correct
22 Correct 62 ms 9932 KB Output is correct
23 Correct 9 ms 1364 KB Output is correct
24 Correct 27 ms 3784 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 7 ms 1276 KB Output is correct
# Verdict Execution time Memory 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 376 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 360 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 35 ms 5004 KB Output is correct
18 Correct 34 ms 5560 KB Output is correct
19 Correct 28 ms 5576 KB Output is correct
20 Correct 23 ms 5184 KB Output is correct
21 Correct 22 ms 5188 KB Output is correct
22 Correct 62 ms 9932 KB Output is correct
23 Correct 9 ms 1364 KB Output is correct
24 Correct 27 ms 3784 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 7 ms 1276 KB Output is correct
27 Correct 3 ms 1492 KB Output is correct
28 Correct 169 ms 27356 KB Output is correct
29 Correct 244 ms 36316 KB Output is correct
30 Correct 274 ms 41360 KB Output is correct
31 Correct 287 ms 41308 KB Output is correct
32 Correct 227 ms 39752 KB Output is correct
33 Correct 315 ms 41308 KB Output is correct
34 Correct 294 ms 41292 KB Output is correct
35 Correct 91 ms 17232 KB Output is correct
36 Correct 288 ms 39708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 35484 KB Output is correct
2 Correct 57 ms 35408 KB Output is correct
3 Correct 105 ms 36000 KB Output is correct
4 Correct 78 ms 38220 KB Output is correct
5 Correct 119 ms 42532 KB Output is correct
6 Correct 122 ms 42556 KB Output is correct
7 Correct 132 ms 42548 KB Output is correct
8 Correct 157 ms 42568 KB Output is correct
9 Correct 126 ms 42448 KB Output is correct
10 Correct 91 ms 25732 KB Output is correct
11 Correct 217 ms 51356 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 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 54 ms 35488 KB Output is correct
19 Correct 61 ms 35500 KB Output is correct
20 Correct 56 ms 35400 KB Output is correct
21 Correct 64 ms 35508 KB Output is correct
22 Correct 170 ms 46852 KB Output is correct
23 Correct 263 ms 62976 KB Output is correct
24 Correct 248 ms 64752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 37328 KB Output is correct
2 Correct 99 ms 42092 KB Output is correct
3 Correct 64 ms 35404 KB Output is correct
4 Correct 61 ms 35436 KB Output is correct
5 Correct 300 ms 64588 KB Output is correct
6 Correct 370 ms 71780 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 188 ms 45248 KB Output is correct
9 Correct 219 ms 51640 KB Output is correct
10 Correct 104 ms 37308 KB Output is correct
11 Correct 104 ms 42168 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 55 ms 35424 KB Output is correct
17 Correct 54 ms 35440 KB Output is correct
18 Correct 115 ms 39768 KB Output is correct
19 Correct 121 ms 44976 KB Output is correct
20 Correct 102 ms 38680 KB Output is correct
21 Correct 133 ms 42868 KB Output is correct
22 Correct 111 ms 38608 KB Output is correct
23 Correct 109 ms 42768 KB Output is correct
24 Correct 54 ms 35484 KB Output is correct
25 Correct 57 ms 35408 KB Output is correct
26 Correct 105 ms 36000 KB Output is correct
27 Correct 78 ms 38220 KB Output is correct
28 Correct 119 ms 42532 KB Output is correct
29 Correct 122 ms 42556 KB Output is correct
30 Correct 132 ms 42548 KB Output is correct
31 Correct 157 ms 42568 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 376 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 360 KB Output is correct
47 Correct 2 ms 544 KB Output is correct
48 Correct 35 ms 5004 KB Output is correct
49 Correct 34 ms 5560 KB Output is correct
50 Correct 28 ms 5576 KB Output is correct
51 Correct 23 ms 5184 KB Output is correct
52 Correct 22 ms 5188 KB Output is correct
53 Correct 62 ms 9932 KB Output is correct
54 Correct 9 ms 1364 KB Output is correct
55 Correct 27 ms 3784 KB Output is correct
56 Correct 2 ms 468 KB Output is correct
57 Correct 7 ms 1276 KB Output is correct
58 Correct 3 ms 1492 KB Output is correct
59 Correct 169 ms 27356 KB Output is correct
60 Correct 244 ms 36316 KB Output is correct
61 Correct 274 ms 41360 KB Output is correct
62 Correct 287 ms 41308 KB Output is correct
63 Correct 227 ms 39752 KB Output is correct
64 Correct 315 ms 41308 KB Output is correct
65 Correct 294 ms 41292 KB Output is correct
66 Correct 91 ms 17232 KB Output is correct
67 Correct 288 ms 39708 KB Output is correct
68 Correct 126 ms 42448 KB Output is correct
69 Correct 91 ms 25732 KB Output is correct
70 Correct 217 ms 51356 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 1 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 54 ms 35488 KB Output is correct
78 Correct 61 ms 35500 KB Output is correct
79 Correct 56 ms 35400 KB Output is correct
80 Correct 64 ms 35508 KB Output is correct
81 Correct 170 ms 46852 KB Output is correct
82 Correct 263 ms 62976 KB Output is correct
83 Correct 248 ms 64752 KB Output is correct
84 Correct 405 ms 66808 KB Output is correct
85 Correct 351 ms 70488 KB Output is correct
86 Correct 379 ms 74688 KB Output is correct
87 Correct 418 ms 78284 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 415 ms 78340 KB Output is correct
90 Correct 320 ms 74656 KB Output is correct
91 Correct 260 ms 70508 KB Output is correct