Submission #745580

# Submission time Handle Problem Language Result Execution time Memory
745580 2023-05-20T12:33:57 Z doowey Catfish Farm (IOI22_fish) C++17
100 / 100
323 ms 50324 KB
#include <bits/stdc++.h>
#include "fish.h"

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

#define fi first
#define se second
#define mp make_pair

const int N = (int)1e5 + 10;

vector<int> cand[N];
vector<pii> fish[N];
vector<ll> fish_cum[N];
vector<pii> dp[N];

int fin_index(int id, int h){
    int ii = lower_bound(cand[id].begin(), cand[id].end(), h) - cand[id].begin();
    if(ii >= cand[id].size() || cand[id][ii] != h) return -1;
    else return ii;
}

void maxi(ll &u, ll v){
    u=max(u, v);
}

ll get_sum(int id, ll pre){
    int jj = lower_bound(fish[id].begin(), fish[id].end(), mp(pre + 1, -1ll)) - fish[id].begin();
    jj -- ;
    if(jj < 0) return 0ll;
    return fish_cum[id][jj];
}

ll F[N][2];
ll Q[N];

ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W) {
    for(auto &x : X){
        x ++ ;
    } // assume x is in range [1; n]
    for(int i = 0 ; i < m ; i ++ ){
        if(Y[i] > 0){
            cand[X[i]].push_back(Y[i] - 1);
        }
        fish[X[i]].push_back(mp(Y[i], W[i]));
        //Q[X[i]] = W[i];
    }
    ll tot;
    for(int i = 0; i <= n; i ++ ){
        cand[i].push_back(n - 1);
        cand[i].push_back(-1);
        sort(cand[i].begin(), cand[i].end());
        dp[i].resize(cand[i].size(), mp(-(ll)1e16, -(ll)1e16));
        sort(fish[i].begin(), fish[i].end());
        for(auto x : fish[i]){
            tot = x.se;
            if(!fish_cum[i].empty()) tot += fish_cum[i].back();
            fish_cum[i].push_back(tot);
        }

    }
    dp[0][0] = mp(0ll,0ll);
    ll meow = 0ll;
    // .fi means increasing direction, .se means decreasing direction
    int las;
    ll val;
    int pp;
    ll take;
    ll current;
    for(int i = 1; i <= n; i ++){
        // ----------------- SAME HEIGHT HANDLING -----------
        for(int j = 0 ;j < cand[i].size(); j ++ ){
            las = fin_index(i - 1, cand[i][j]);
            if(las != -1){
                val = max(dp[i - 1][las].fi, dp[i - 1][las].se);
                maxi(dp[i][j].fi, val);
                maxi(dp[i][j].se, val);
            }
        }

        // DECREASING HEIGHT HANDLING
        pp = cand[i - 1].size() - 1;
        take = 0ll;
        for(int j = cand[i].size() - 1; j >= 0 ; j -- ){
            while(pp >= 0 && cand[i - 1][pp] > cand[i][j]){
                ll A = max(dp[i - 1][pp].fi, dp[i - 1][pp].se);
                ll B = get_sum(i, cand[i - 1][pp]);
                current = (ll)max(dp[i - 1][pp].fi, dp[i - 1][pp].se) + (ll)get_sum(i, cand[i - 1][pp]);
                maxi(take, current);
                pp -- ;
            }
            current = take - get_sum(i, cand[i][j]);
            maxi(dp[i][j].se, current);
        }
        // INCREASING HEIGHT HANDLING

        pp = 0; // transition from 0 is different

        take = -(ll)1e18;
        for(int j = 0; j < cand[i].size(); j ++ ){
            while(pp < cand[i - 1].size() && cand[i - 1][pp] <= cand[i][j]){
                maxi(take, dp[i - 1][pp].fi - get_sum(i - 1, cand[i - 1][pp]));
                pp ++ ;
            }
            current = take + get_sum(i - 1, cand[i][j]);
            maxi(dp[i][j].fi, current);
            maxi(dp[i][j].se, current);

        }

        // INCREASING HEIGHT IF PREV IS EMPTY

        pp = 0;
        take = 0ll;
        for(int j = 0 ; j < cand[i].size(); j ++ ){
            while(i >= 2 && pp < cand[i - 2].size() && cand[i - 2][pp] <= cand[i][j]){
                maxi(take, dp[i - 2][pp].fi);
                maxi(take, dp[i - 2][pp].se);
                pp ++ ;
            }
            current = take + get_sum(i - 1, cand[i][j]);
            maxi(dp[i][j].fi, current);
            maxi(dp[i][j].se, current);
        }
        if(i >= 2){
            pp = cand[i - 2].size() - 1;
            take = 0ll;
            for(int j = cand[i].size() - 1; j >= 0 ; j --  ){
                while(pp >= 0 && cand[i - 2][pp] > cand[i][j]){
                    maxi(take, max(dp[i - 2][pp].fi, dp[i - 2][pp].se) + get_sum(i - 1, cand[i - 2][pp]));
                    pp -- ;
                }
                maxi(dp[i][j].fi, take);
                maxi(dp[i][j].se, take);
            }
        }
        for(int j = 0 ; j < cand[i].size(); j ++ ){
            maxi(meow, dp[i][j].fi);
            maxi(meow, dp[i][j].se);
        }
        /*
        cout << i << ": ";
        for(int j = 0; j < cand[i].size(); j ++ ){
            cout << "(" << cand[i][j] << ": " << dp[i][j].fi << " : " << dp[i][j].se << ") ";
        }
        cout << "\n";
        */
    }
    for(int i = 2; i <= n; i ++ ){
        // i taken
        F[i][1] = max(Q[i - 1] + max(F[i - 2][0], F[i - 2][1]), F[i - 1][1]);
        F[i][0] = max(Q[i] + F[i - 1][1], F[i - 1][0]);
    }
    //ll chomp = max(F[n][1], F[n][0]);
    //cout << meow << "/" << chomp << "\n";
    return meow;
}

Compilation message

fish.cpp: In function 'int fin_index(int, int)':
fish.cpp:22:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     if(ii >= cand[id].size() || cand[id][ii] != h) return -1;
      |        ~~~^~~~~~~~~~~~~~~~~~
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:75:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for(int j = 0 ;j < cand[i].size(); j ++ ){
      |                        ~~^~~~~~~~~~~~~~~~
fish.cpp:89:20: warning: unused variable 'A' [-Wunused-variable]
   89 |                 ll A = max(dp[i - 1][pp].fi, dp[i - 1][pp].se);
      |                    ^
fish.cpp:90:20: warning: unused variable 'B' [-Wunused-variable]
   90 |                 ll B = get_sum(i, cand[i - 1][pp]);
      |                    ^
fish.cpp:103:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for(int j = 0; j < cand[i].size(); j ++ ){
      |                        ~~^~~~~~~~~~~~~~~~
fish.cpp:104:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |             while(pp < cand[i - 1].size() && cand[i - 1][pp] <= cand[i][j]){
      |                   ~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:118:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for(int j = 0 ; j < cand[i].size(); j ++ ){
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:119:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |             while(i >= 2 && pp < cand[i - 2].size() && cand[i - 2][pp] <= cand[i][j]){
      |                             ~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:140:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |         for(int j = 0 ; j < cand[i].size(); j ++ ){
      |                         ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 76 ms 23476 KB Output is correct
2 Correct 88 ms 25964 KB Output is correct
3 Correct 29 ms 18976 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 249 ms 40320 KB Output is correct
6 Correct 280 ms 43508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 149 ms 29368 KB Output is correct
3 Correct 185 ms 32772 KB Output is correct
4 Correct 82 ms 23672 KB Output is correct
5 Correct 90 ms 25980 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9748 KB Output is correct
9 Correct 6 ms 9700 KB Output is correct
10 Correct 31 ms 19168 KB Output is correct
11 Correct 32 ms 19028 KB Output is correct
12 Correct 81 ms 23828 KB Output is correct
13 Correct 90 ms 25984 KB Output is correct
14 Correct 78 ms 23776 KB Output is correct
15 Correct 104 ms 25348 KB Output is correct
16 Correct 86 ms 23852 KB Output is correct
17 Correct 89 ms 25328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 19028 KB Output is correct
2 Correct 29 ms 19056 KB Output is correct
3 Correct 60 ms 22684 KB Output is correct
4 Correct 48 ms 22132 KB Output is correct
5 Correct 82 ms 27640 KB Output is correct
6 Correct 80 ms 27596 KB Output is correct
7 Correct 96 ms 27676 KB Output is correct
8 Correct 90 ms 27744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9668 KB Output is correct
4 Correct 6 ms 9700 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9704 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9940 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9668 KB Output is correct
4 Correct 6 ms 9700 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9704 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9940 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9712 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 7 ms 10012 KB Output is correct
17 Correct 41 ms 13804 KB Output is correct
18 Correct 38 ms 14676 KB Output is correct
19 Correct 33 ms 14432 KB Output is correct
20 Correct 31 ms 14412 KB Output is correct
21 Correct 32 ms 14428 KB Output is correct
22 Correct 62 ms 19248 KB Output is correct
23 Correct 11 ms 10592 KB Output is correct
24 Correct 26 ms 12504 KB Output is correct
25 Correct 7 ms 9812 KB Output is correct
26 Correct 11 ms 10456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9668 KB Output is correct
4 Correct 6 ms 9700 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9704 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9940 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9712 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 7 ms 10012 KB Output is correct
17 Correct 41 ms 13804 KB Output is correct
18 Correct 38 ms 14676 KB Output is correct
19 Correct 33 ms 14432 KB Output is correct
20 Correct 31 ms 14412 KB Output is correct
21 Correct 32 ms 14428 KB Output is correct
22 Correct 62 ms 19248 KB Output is correct
23 Correct 11 ms 10592 KB Output is correct
24 Correct 26 ms 12504 KB Output is correct
25 Correct 7 ms 9812 KB Output is correct
26 Correct 11 ms 10456 KB Output is correct
27 Correct 8 ms 10224 KB Output is correct
28 Correct 175 ms 31948 KB Output is correct
29 Correct 242 ms 39016 KB Output is correct
30 Correct 213 ms 38412 KB Output is correct
31 Correct 222 ms 38420 KB Output is correct
32 Correct 215 ms 41560 KB Output is correct
33 Correct 236 ms 38416 KB Output is correct
34 Correct 216 ms 38444 KB Output is correct
35 Correct 85 ms 21568 KB Output is correct
36 Correct 244 ms 40836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 19028 KB Output is correct
2 Correct 29 ms 19056 KB Output is correct
3 Correct 60 ms 22684 KB Output is correct
4 Correct 48 ms 22132 KB Output is correct
5 Correct 82 ms 27640 KB Output is correct
6 Correct 80 ms 27596 KB Output is correct
7 Correct 96 ms 27676 KB Output is correct
8 Correct 90 ms 27744 KB Output is correct
9 Correct 106 ms 29312 KB Output is correct
10 Correct 81 ms 23088 KB Output is correct
11 Correct 167 ms 36604 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 5 ms 9656 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9700 KB Output is correct
18 Correct 30 ms 19080 KB Output is correct
19 Correct 28 ms 19100 KB Output is correct
20 Correct 31 ms 19028 KB Output is correct
21 Correct 31 ms 19080 KB Output is correct
22 Correct 113 ms 29772 KB Output is correct
23 Correct 201 ms 38152 KB Output is correct
24 Correct 199 ms 38780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 23476 KB Output is correct
2 Correct 88 ms 25964 KB Output is correct
3 Correct 29 ms 18976 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 249 ms 40320 KB Output is correct
6 Correct 280 ms 43508 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 149 ms 29368 KB Output is correct
9 Correct 185 ms 32772 KB Output is correct
10 Correct 82 ms 23672 KB Output is correct
11 Correct 90 ms 25980 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 6 ms 9748 KB Output is correct
15 Correct 6 ms 9700 KB Output is correct
16 Correct 31 ms 19168 KB Output is correct
17 Correct 32 ms 19028 KB Output is correct
18 Correct 81 ms 23828 KB Output is correct
19 Correct 90 ms 25984 KB Output is correct
20 Correct 78 ms 23776 KB Output is correct
21 Correct 104 ms 25348 KB Output is correct
22 Correct 86 ms 23852 KB Output is correct
23 Correct 89 ms 25328 KB Output is correct
24 Correct 28 ms 19028 KB Output is correct
25 Correct 29 ms 19056 KB Output is correct
26 Correct 60 ms 22684 KB Output is correct
27 Correct 48 ms 22132 KB Output is correct
28 Correct 82 ms 27640 KB Output is correct
29 Correct 80 ms 27596 KB Output is correct
30 Correct 96 ms 27676 KB Output is correct
31 Correct 90 ms 27744 KB Output is correct
32 Correct 5 ms 9692 KB Output is correct
33 Correct 7 ms 9684 KB Output is correct
34 Correct 6 ms 9668 KB Output is correct
35 Correct 6 ms 9700 KB Output is correct
36 Correct 5 ms 9684 KB Output is correct
37 Correct 5 ms 9684 KB Output is correct
38 Correct 6 ms 9704 KB Output is correct
39 Correct 5 ms 9684 KB Output is correct
40 Correct 6 ms 9684 KB Output is correct
41 Correct 7 ms 9940 KB Output is correct
42 Correct 6 ms 9684 KB Output is correct
43 Correct 7 ms 9812 KB Output is correct
44 Correct 5 ms 9684 KB Output is correct
45 Correct 6 ms 9712 KB Output is correct
46 Correct 7 ms 9684 KB Output is correct
47 Correct 7 ms 10012 KB Output is correct
48 Correct 41 ms 13804 KB Output is correct
49 Correct 38 ms 14676 KB Output is correct
50 Correct 33 ms 14432 KB Output is correct
51 Correct 31 ms 14412 KB Output is correct
52 Correct 32 ms 14428 KB Output is correct
53 Correct 62 ms 19248 KB Output is correct
54 Correct 11 ms 10592 KB Output is correct
55 Correct 26 ms 12504 KB Output is correct
56 Correct 7 ms 9812 KB Output is correct
57 Correct 11 ms 10456 KB Output is correct
58 Correct 8 ms 10224 KB Output is correct
59 Correct 175 ms 31948 KB Output is correct
60 Correct 242 ms 39016 KB Output is correct
61 Correct 213 ms 38412 KB Output is correct
62 Correct 222 ms 38420 KB Output is correct
63 Correct 215 ms 41560 KB Output is correct
64 Correct 236 ms 38416 KB Output is correct
65 Correct 216 ms 38444 KB Output is correct
66 Correct 85 ms 21568 KB Output is correct
67 Correct 244 ms 40836 KB Output is correct
68 Correct 106 ms 29312 KB Output is correct
69 Correct 81 ms 23088 KB Output is correct
70 Correct 167 ms 36604 KB Output is correct
71 Correct 5 ms 9684 KB Output is correct
72 Correct 7 ms 9684 KB Output is correct
73 Correct 6 ms 9684 KB Output is correct
74 Correct 5 ms 9656 KB Output is correct
75 Correct 5 ms 9684 KB Output is correct
76 Correct 5 ms 9700 KB Output is correct
77 Correct 30 ms 19080 KB Output is correct
78 Correct 28 ms 19100 KB Output is correct
79 Correct 31 ms 19028 KB Output is correct
80 Correct 31 ms 19080 KB Output is correct
81 Correct 113 ms 29772 KB Output is correct
82 Correct 201 ms 38152 KB Output is correct
83 Correct 199 ms 38780 KB Output is correct
84 Correct 263 ms 44572 KB Output is correct
85 Correct 284 ms 45004 KB Output is correct
86 Correct 323 ms 49204 KB Output is correct
87 Correct 296 ms 50324 KB Output is correct
88 Correct 5 ms 9684 KB Output is correct
89 Correct 304 ms 50280 KB Output is correct
90 Correct 271 ms 48136 KB Output is correct
91 Correct 233 ms 47620 KB Output is correct