Submission #1090220

# Submission time Handle Problem Language Result Execution time Memory
1090220 2024-09-18T03:40:02 Z steveonalex Catfish Farm (IOI22_fish) C++17
100 / 100
529 ms 84328 KB
#include <bits/stdc++.h>
#include "fish.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


const ll INF = 1e18 + 69;

int n, m;
ll max_weights(int _n, int _m, vector<int> X, vector<int> Y, vector<int> W) {
    n = _n, m = _m;
    for(int i = 0; i<m; ++i){
        X[i]++; Y[i]++;
    }

    vector<int> coord[n + 2];
    for(int i = 0; i<m; ++i){
        for(int j = X[i] - 1; j <= X[i] + 1; ++j){
            coord[j].push_back(Y[i]);
            coord[j].push_back(Y[i]-1);
        }
    }

    vector<ll> grid[n+2];
    for(int i= 0; i<=n; ++i){
        coord[i].push_back(0); coord[i].push_back(n);
        remove_dup(coord[i]);
        grid[i].resize(coord[i].size());
    }

    for(int i = 0; i<m; ++i){
        int j = lower_bound(ALL(coord[X[i]]), Y[i]) - coord[X[i]].begin();
        grid[X[i]][j] += W[i];
    }

    for(int i = 1; i<=n; ++i){
        for(int j = 1; j < (int)grid[i].size(); ++j) 
            grid[i][j] += grid[i][j-1];
    }

    vector<array<ll, 2>> dp[n+2];
    for(int i = 0; i<=n; ++i){
        dp[i].resize(coord[i].size(), {{-INF, -INF}});
    }
    // // dp[n][m][ascending];
    dp[0][0][1] = 0;
    for(int i = 1; i<=n; ++i){
        maximize(dp[i][0][1], dp[i-1][0][0]);
        // increase
        int len = coord[i].size();
        block_of_code{
            int _len = coord[i-1].size();
            ll pref[len], sum[len]; 
            fill(pref, pref + len, -INF);
            fill(sum, sum + len, -INF);
            for(int j = 0; j < _len; ++j){
                int idx = lower_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin();
                ll val = dp[i-1][j][1] - grid[i-1][j];
                maximize(pref[idx], val);
                maximize(sum[idx], grid[i-1][j]);
            }
            for(int j = 1; j < len; ++j) {
                maximize(pref[j], pref[j-1]);
                maximize(sum[j], sum[j-1]);
            }
            for(int j = 0; j < len; ++j){
                maximize(dp[i][j][1], pref[j] + sum[j]);
            }
        }

        // decrease
        block_of_code{
            int _len = coord[i-1].size();
            ll suff[len]; fill(suff, suff + len, -INF);
            for(int j = 0; j < _len; ++j){
                int idx = upper_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin() - 1;
                ll val = max(dp[i-1][j][0], dp[i-1][j][1]);
                maximize(suff[idx], val);
            }
            for(int j = 0; j < len; ++j) suff[j] += grid[i][j];
            for(int j = len - 1; j >= 1; --j) maximize(suff[j-1], suff[j]);

            for(int j = len-1; j >= 0; --j){
                maximize(dp[i][j][0], suff[j] - grid[i][j]);
            }
        }


        // jump two
        if (i >= 2){
            ll pref[len], suff[len], sum[len];
            fill(pref, pref + len, -INF);
            fill(suff, suff + len, -INF);
            fill(sum, sum + len, 0);

            int _len = coord[i-2].size();
            for(int j = 0; j < _len; ++j){
                ll val = max(dp[i-2][j][0], dp[i-2][j][1]);
                int idx = lower_bound(ALL(coord[i]), coord[i-2][j]) - coord[i].begin();
                maximize(pref[idx], val);

                idx = upper_bound(ALL(coord[i]), coord[i-2][j]) - coord[i].begin() - 1;
                maximize(suff[idx], val);
            }


            for(int j = 0; j < (int) coord[i-1].size(); ++j){
                int idx = lower_bound(ALL(coord[i]), coord[i-1][j]) - coord[i].begin();
                sum[idx] = grid[i-1][j];
            }
            for(int j = 1; j < len; ++j) {
                maximize(sum[j], sum[j-1]);
                maximize(pref[j], pref[j-1]);
            }
            for(int j = len-1; j>=0; --j){
                suff[j] += sum[j];
                if (j < len - 1) maximize(suff[j], suff[j+1]);
            }

            for(int j = 0; j<len; ++j){
                maximize(dp[i][j][1], max(pref[j] + sum[j], suff[j]));
            }
        }

        // cout << "Dp = " << i << "\n";
        // for(auto j: dp[i]) printArr(j);
    }


    ll ans = 0;
    for(int j = 0; j < coord[n].size(); ++j) for(int x = 0; x <= 1; ++x) maximize(ans, dp[n][j][x]);
    return ans;
}


// int main(void){
//     ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
//     clock_t start = clock();

//     int n, m; cin >> n >> m;
//     vector<int> X(m), Y(m), W(m);
//     for(int i = 0; i<m; ++i){
//         cin >> X[i] >> Y[i] >> W[i];
//     }

//     cout << max_weights(n, m, X, Y, W) << "\n";


//     cerr << "Time elapsed: " << clock() - start << " ms\n";
//     return 0;
// }

Compilation message

fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:181:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |     for(int j = 0; j < coord[n].size(); ++j) for(int x = 0; x <= 1; ++x) maximize(ans, dp[n][j][x]);
      |                    ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 111 ms 30392 KB Output is correct
2 Correct 127 ms 34228 KB Output is correct
3 Correct 26 ms 18268 KB Output is correct
4 Correct 28 ms 18272 KB Output is correct
5 Correct 375 ms 64408 KB Output is correct
6 Correct 357 ms 83796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 183 ms 37448 KB Output is correct
3 Correct 231 ms 42872 KB Output is correct
4 Correct 125 ms 30140 KB Output is correct
5 Correct 128 ms 34232 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 26 ms 18268 KB Output is correct
11 Correct 36 ms 18260 KB Output is correct
12 Correct 111 ms 30140 KB Output is correct
13 Correct 143 ms 34236 KB Output is correct
14 Correct 111 ms 30240 KB Output is correct
15 Correct 111 ms 30008 KB Output is correct
16 Correct 126 ms 30164 KB Output is correct
17 Correct 123 ms 33332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 18264 KB Output is correct
2 Correct 32 ms 18344 KB Output is correct
3 Correct 48 ms 20564 KB Output is correct
4 Correct 42 ms 21076 KB Output is correct
5 Correct 67 ms 25680 KB Output is correct
6 Correct 69 ms 25172 KB Output is correct
7 Correct 72 ms 25936 KB Output is correct
8 Correct 74 ms 25840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 28 ms 5328 KB Output is correct
18 Correct 28 ms 4700 KB Output is correct
19 Correct 26 ms 4696 KB Output is correct
20 Correct 28 ms 4432 KB Output is correct
21 Correct 25 ms 4444 KB Output is correct
22 Correct 51 ms 8528 KB Output is correct
23 Correct 10 ms 2192 KB Output is correct
24 Correct 24 ms 4940 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 8 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 28 ms 5328 KB Output is correct
18 Correct 28 ms 4700 KB Output is correct
19 Correct 26 ms 4696 KB Output is correct
20 Correct 28 ms 4432 KB Output is correct
21 Correct 25 ms 4444 KB Output is correct
22 Correct 51 ms 8528 KB Output is correct
23 Correct 10 ms 2192 KB Output is correct
24 Correct 24 ms 4940 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 8 ms 1884 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 174 ms 21672 KB Output is correct
29 Correct 273 ms 34384 KB Output is correct
30 Correct 481 ms 63060 KB Output is correct
31 Correct 480 ms 62444 KB Output is correct
32 Correct 178 ms 28652 KB Output is correct
33 Correct 516 ms 65364 KB Output is correct
34 Correct 529 ms 65360 KB Output is correct
35 Correct 87 ms 17492 KB Output is correct
36 Correct 434 ms 49428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 18264 KB Output is correct
2 Correct 32 ms 18344 KB Output is correct
3 Correct 48 ms 20564 KB Output is correct
4 Correct 42 ms 21076 KB Output is correct
5 Correct 67 ms 25680 KB Output is correct
6 Correct 69 ms 25172 KB Output is correct
7 Correct 72 ms 25936 KB Output is correct
8 Correct 74 ms 25840 KB Output is correct
9 Correct 85 ms 33620 KB Output is correct
10 Correct 57 ms 18260 KB Output is correct
11 Correct 119 ms 36308 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 28 ms 18236 KB Output is correct
19 Correct 28 ms 18264 KB Output is correct
20 Correct 28 ms 18268 KB Output is correct
21 Correct 29 ms 18388 KB Output is correct
22 Correct 135 ms 39764 KB Output is correct
23 Correct 148 ms 42320 KB Output is correct
24 Correct 168 ms 43860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 30392 KB Output is correct
2 Correct 127 ms 34228 KB Output is correct
3 Correct 26 ms 18268 KB Output is correct
4 Correct 28 ms 18272 KB Output is correct
5 Correct 375 ms 64408 KB Output is correct
6 Correct 357 ms 83796 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 183 ms 37448 KB Output is correct
9 Correct 231 ms 42872 KB Output is correct
10 Correct 125 ms 30140 KB Output is correct
11 Correct 128 ms 34232 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 26 ms 18268 KB Output is correct
17 Correct 36 ms 18260 KB Output is correct
18 Correct 111 ms 30140 KB Output is correct
19 Correct 143 ms 34236 KB Output is correct
20 Correct 111 ms 30240 KB Output is correct
21 Correct 111 ms 30008 KB Output is correct
22 Correct 126 ms 30164 KB Output is correct
23 Correct 123 ms 33332 KB Output is correct
24 Correct 32 ms 18264 KB Output is correct
25 Correct 32 ms 18344 KB Output is correct
26 Correct 48 ms 20564 KB Output is correct
27 Correct 42 ms 21076 KB Output is correct
28 Correct 67 ms 25680 KB Output is correct
29 Correct 69 ms 25172 KB Output is correct
30 Correct 72 ms 25936 KB Output is correct
31 Correct 74 ms 25840 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 444 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 2 ms 504 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 28 ms 5328 KB Output is correct
49 Correct 28 ms 4700 KB Output is correct
50 Correct 26 ms 4696 KB Output is correct
51 Correct 28 ms 4432 KB Output is correct
52 Correct 25 ms 4444 KB Output is correct
53 Correct 51 ms 8528 KB Output is correct
54 Correct 10 ms 2192 KB Output is correct
55 Correct 24 ms 4940 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 8 ms 1884 KB Output is correct
58 Correct 3 ms 1372 KB Output is correct
59 Correct 174 ms 21672 KB Output is correct
60 Correct 273 ms 34384 KB Output is correct
61 Correct 481 ms 63060 KB Output is correct
62 Correct 480 ms 62444 KB Output is correct
63 Correct 178 ms 28652 KB Output is correct
64 Correct 516 ms 65364 KB Output is correct
65 Correct 529 ms 65360 KB Output is correct
66 Correct 87 ms 17492 KB Output is correct
67 Correct 434 ms 49428 KB Output is correct
68 Correct 85 ms 33620 KB Output is correct
69 Correct 57 ms 18260 KB Output is correct
70 Correct 119 ms 36308 KB Output is correct
71 Correct 1 ms 344 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 28 ms 18236 KB Output is correct
78 Correct 28 ms 18264 KB Output is correct
79 Correct 28 ms 18268 KB Output is correct
80 Correct 29 ms 18388 KB Output is correct
81 Correct 135 ms 39764 KB Output is correct
82 Correct 148 ms 42320 KB Output is correct
83 Correct 168 ms 43860 KB Output is correct
84 Correct 474 ms 68724 KB Output is correct
85 Correct 497 ms 68992 KB Output is correct
86 Correct 356 ms 81744 KB Output is correct
87 Correct 370 ms 84328 KB Output is correct
88 Correct 1 ms 604 KB Output is correct
89 Correct 362 ms 84184 KB Output is correct
90 Correct 241 ms 61012 KB Output is correct
91 Correct 192 ms 49644 KB Output is correct