Submission #630110

# Submission time Handle Problem Language Result Execution time Memory
630110 2022-08-15T17:07:16 Z CyanForces Catfish Farm (IOI22_fish) C++17
100 / 100
551 ms 61296 KB
#include "fish.h"

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef long double ld;


bool smin(auto &a, auto&& b) { return (b < a) ? (a = b, 1) : 0; } 
bool smax(auto &a, auto&& b) { return (a < b) ? (a = b, 1) : 0; } 

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
    std::vector<int> W) {

  int n = N, m = M;
  vi x(all(X)), y(all(Y));
  vi w(all(W));

  vector<vi> at_x(n);
  vector<vi> at_dp(n);
  rep(i,0,n) at_dp[i].emplace_back(-1);
  rep(i,0,m) {
    at_x[x[i]].emplace_back(i);
    if(x[i]) at_dp[x[i]-1].emplace_back(y[i]);
    if(x[i]+1 < n) at_dp[x[i]+1].emplace_back(y[i]);
  }
  rep(i,0,n) sort(all(at_dp[i]));

  vector<vi> at_x_pref(n);
  rep(i,0,n) {
    sort(all(at_x[i]), [&](int a, int b) { return y[a] < y[b]; });
    at_x_pref[i].resize(sz(at_x[i])+1);
    rep(j,0,sz(at_x[i])) at_x_pref[i][j+1] = at_x_pref[i][j] + w[at_x[i][j]];
  }

  auto fish_ = [&](int x, int h) -> ll { // exclusive
    int l = -1, r = sz(at_x[x]);
    while(l+1 < r) {
      int m = (l+r)/2;
      if(h <= y[at_x[x][m]]) r = m;
      else l = m;
    }
    return at_x_pref[x][r];
  };

  auto fish = [&](int x, int h0, int h1) -> ll { // inclusive
    ll res2 = fish_(x,h1+1) - fish_(x,h0);
    //ll res = 0;
    //for(int i : at_x[x]) if(h0 <= y[i] && y[i] <= h1) res += w[i];
    //assert(res == res2);
    return res2;
  };

  vector<vi> dp_inc(n), dp_dec(n);
  rep(i,0,n) {
    vi& cur_inc = dp_inc[i];
    vi& cur_dec = dp_dec[i];
    cur_inc.assign(sz(at_dp[i]),0);
    cur_dec.assign(sz(at_dp[i]),0);

    if(i == 0) continue;

    vi& prev_inc = dp_inc[i-1];
    vi& prev_dec = dp_dec[i-1];

    {
      int jp = 0;
      ll opt = -1e18;
      rep(j,0,sz(cur_inc)) {
        int h_cur = at_dp[i][j];
        while(jp < sz(at_dp[i-1]) && at_dp[i-1][jp] <= h_cur) {
          int h_prev = at_dp[i-1][jp];
          assert(h_prev <= h_cur);
          smax(opt, prev_inc[jp] - fish(i-1,0,h_prev));
          ++jp;
        }
        smax(cur_inc[j], opt + fish(i-1,0,h_cur));
      }
    }

    {
      int jp = sz(prev_dec);
      ll opt = -1e18;
      for(int j = sz(cur_dec)-1; j >= 0; --j) {
        int h_cur = at_dp[i][j];
        while(jp && at_dp[i-1][jp-1] >= h_cur) {
          --jp;
          int h_prev = at_dp[i-1][jp];
          assert(h_prev >= h_cur);
          smax(opt, max(prev_dec[jp], prev_inc[jp]) + fish(i,0,h_prev));
        }
        smax(cur_dec[j], opt - fish(i,0,h_cur));
      }
    }

    if(i == 1) continue;

    for(auto prev : {dp_inc[i-2], dp_dec[i-2]}) {
      //rep(j,0,sz(at_dp[i])) {
      //  rep(jp,0,sz(prev)) {
      //    int h_cur = at_dp[i][j];
      //    int h_prev = at_dp[i-2][jp];
      //    ll q = prev[jp] + fish(i-1,0,max(h_prev, h_cur));
      //    smax(cur_inc[j], q);
      //    smax(cur_dec[j], q);
      //  }
      //}

      {
        int jp = 0;
        ll opt = -1e18;
        rep(j,0,sz(cur_inc)) {
          int h_cur = at_dp[i][j];
          while(jp < sz(at_dp[i-2]) && at_dp[i-2][jp] <= h_cur) {
            int h_prev = at_dp[i-2][jp];
            assert(h_prev <= h_cur);
            smax(opt, prev[jp]);
            ++jp;
          }
          smax(cur_dec[j], opt + fish(i-1,0,h_cur));
          smax(cur_inc[j], opt + fish(i-1,0,h_cur));
        }
      }
      {
        int jp = sz(prev);
        ll opt = -1e18;
        for(int j = sz(cur_dec)-1; j >= 0; --j) {
          int h_cur = at_dp[i][j];
          while(jp && at_dp[i-2][jp-1] >= h_cur) {
            --jp;
            int h_prev = at_dp[i-2][jp];
            assert(h_prev >= h_cur);
            smax(opt, prev[jp] + fish(i-1,0,h_prev));
          }
          smax(cur_dec[j], opt);
          smax(cur_inc[j], opt);
        }
      }
    }
  }

  ll ans = 0;
  ans = max(ans, *max_element(all(dp_inc[n-1])));
  ans = max(ans, *max_element(all(dp_dec[n-1])));
  if(n >= 2) {
    rep(jp,0,sz(at_dp[n-2])) {
      int h_prev = at_dp[n-2][jp];
      smax(ans, dp_inc[n-2][jp] + fish(n-1,0,h_prev));
      smax(ans, dp_dec[n-2][jp] + fish(n-1,0,h_prev));
    }
  }

  return ans;
}

Compilation message

fish.cpp:16:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   16 | bool smin(auto &a, auto&& b) { return (b < a) ? (a = b, 1) : 0; }
      |           ^~~~
fish.cpp:16:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   16 | bool smin(auto &a, auto&& b) { return (b < a) ? (a = b, 1) : 0; }
      |                    ^~~~
fish.cpp:17:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   17 | bool smax(auto &a, auto&& b) { return (a < b) ? (a = b, 1) : 0; }
      |           ^~~~
fish.cpp:17:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   17 | bool smax(auto &a, auto&& b) { return (a < b) ? (a = b, 1) : 0; }
      |                    ^~~~
# Verdict Execution time Memory Grader output
1 Correct 85 ms 29068 KB Output is correct
2 Correct 101 ms 33088 KB Output is correct
3 Correct 49 ms 24496 KB Output is correct
4 Correct 47 ms 24524 KB Output is correct
5 Correct 443 ms 59288 KB Output is correct
6 Correct 455 ms 58652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 207 ms 38004 KB Output is correct
3 Correct 252 ms 44152 KB Output is correct
4 Correct 89 ms 29056 KB Output is correct
5 Correct 104 ms 33152 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 216 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 45 ms 24552 KB Output is correct
11 Correct 46 ms 24532 KB Output is correct
12 Correct 140 ms 31096 KB Output is correct
13 Correct 165 ms 35556 KB Output is correct
14 Correct 116 ms 30124 KB Output is correct
15 Correct 150 ms 33420 KB Output is correct
16 Correct 122 ms 30144 KB Output is correct
17 Correct 127 ms 33284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 24448 KB Output is correct
2 Correct 51 ms 24500 KB Output is correct
3 Correct 110 ms 27012 KB Output is correct
4 Correct 81 ms 27816 KB Output is correct
5 Correct 188 ms 33928 KB Output is correct
6 Correct 172 ms 33936 KB Output is correct
7 Correct 167 ms 33900 KB Output is correct
8 Correct 183 ms 33896 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 1 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 2 ms 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 0 ms 212 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 1 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 2 ms 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 65 ms 5476 KB Output is correct
18 Correct 71 ms 7056 KB Output is correct
19 Correct 62 ms 6812 KB Output is correct
20 Correct 66 ms 6816 KB Output is correct
21 Correct 64 ms 6780 KB Output is correct
22 Correct 134 ms 13340 KB Output is correct
23 Correct 13 ms 1620 KB Output is correct
24 Correct 47 ms 4296 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 12 ms 1468 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 1 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 2 ms 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 65 ms 5476 KB Output is correct
18 Correct 71 ms 7056 KB Output is correct
19 Correct 62 ms 6812 KB Output is correct
20 Correct 66 ms 6816 KB Output is correct
21 Correct 64 ms 6780 KB Output is correct
22 Correct 134 ms 13340 KB Output is correct
23 Correct 13 ms 1620 KB Output is correct
24 Correct 47 ms 4296 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 12 ms 1468 KB Output is correct
27 Correct 6 ms 1248 KB Output is correct
28 Correct 368 ms 30924 KB Output is correct
29 Correct 493 ms 42656 KB Output is correct
30 Correct 402 ms 41476 KB Output is correct
31 Correct 450 ms 41588 KB Output is correct
32 Correct 484 ms 43300 KB Output is correct
33 Correct 419 ms 41468 KB Output is correct
34 Correct 422 ms 41036 KB Output is correct
35 Correct 146 ms 16728 KB Output is correct
36 Correct 492 ms 41724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 24448 KB Output is correct
2 Correct 51 ms 24500 KB Output is correct
3 Correct 110 ms 27012 KB Output is correct
4 Correct 81 ms 27816 KB Output is correct
5 Correct 188 ms 33928 KB Output is correct
6 Correct 172 ms 33936 KB Output is correct
7 Correct 167 ms 33900 KB Output is correct
8 Correct 183 ms 33896 KB Output is correct
9 Correct 166 ms 33968 KB Output is correct
10 Correct 119 ms 22816 KB Output is correct
11 Correct 287 ms 45540 KB Output is correct
12 Correct 1 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 46 ms 24532 KB Output is correct
19 Correct 45 ms 24512 KB Output is correct
20 Correct 44 ms 24524 KB Output is correct
21 Correct 45 ms 24500 KB Output is correct
22 Correct 174 ms 33788 KB Output is correct
23 Correct 289 ms 45268 KB Output is correct
24 Correct 319 ms 45388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 29068 KB Output is correct
2 Correct 101 ms 33088 KB Output is correct
3 Correct 49 ms 24496 KB Output is correct
4 Correct 47 ms 24524 KB Output is correct
5 Correct 443 ms 59288 KB Output is correct
6 Correct 455 ms 58652 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 207 ms 38004 KB Output is correct
9 Correct 252 ms 44152 KB Output is correct
10 Correct 89 ms 29056 KB Output is correct
11 Correct 104 ms 33152 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 216 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 45 ms 24552 KB Output is correct
17 Correct 46 ms 24532 KB Output is correct
18 Correct 140 ms 31096 KB Output is correct
19 Correct 165 ms 35556 KB Output is correct
20 Correct 116 ms 30124 KB Output is correct
21 Correct 150 ms 33420 KB Output is correct
22 Correct 122 ms 30144 KB Output is correct
23 Correct 127 ms 33284 KB Output is correct
24 Correct 55 ms 24448 KB Output is correct
25 Correct 51 ms 24500 KB Output is correct
26 Correct 110 ms 27012 KB Output is correct
27 Correct 81 ms 27816 KB Output is correct
28 Correct 188 ms 33928 KB Output is correct
29 Correct 172 ms 33936 KB Output is correct
30 Correct 167 ms 33900 KB Output is correct
31 Correct 183 ms 33896 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 1 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 2 ms 596 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 3 ms 604 KB Output is correct
48 Correct 65 ms 5476 KB Output is correct
49 Correct 71 ms 7056 KB Output is correct
50 Correct 62 ms 6812 KB Output is correct
51 Correct 66 ms 6816 KB Output is correct
52 Correct 64 ms 6780 KB Output is correct
53 Correct 134 ms 13340 KB Output is correct
54 Correct 13 ms 1620 KB Output is correct
55 Correct 47 ms 4296 KB Output is correct
56 Correct 2 ms 468 KB Output is correct
57 Correct 12 ms 1468 KB Output is correct
58 Correct 6 ms 1248 KB Output is correct
59 Correct 368 ms 30924 KB Output is correct
60 Correct 493 ms 42656 KB Output is correct
61 Correct 402 ms 41476 KB Output is correct
62 Correct 450 ms 41588 KB Output is correct
63 Correct 484 ms 43300 KB Output is correct
64 Correct 419 ms 41468 KB Output is correct
65 Correct 422 ms 41036 KB Output is correct
66 Correct 146 ms 16728 KB Output is correct
67 Correct 492 ms 41724 KB Output is correct
68 Correct 166 ms 33968 KB Output is correct
69 Correct 119 ms 22816 KB Output is correct
70 Correct 287 ms 45540 KB Output is correct
71 Correct 1 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 46 ms 24532 KB Output is correct
78 Correct 45 ms 24512 KB Output is correct
79 Correct 44 ms 24524 KB Output is correct
80 Correct 45 ms 24500 KB Output is correct
81 Correct 174 ms 33788 KB Output is correct
82 Correct 289 ms 45268 KB Output is correct
83 Correct 319 ms 45388 KB Output is correct
84 Correct 540 ms 59180 KB Output is correct
85 Correct 551 ms 61296 KB Output is correct
86 Correct 459 ms 56212 KB Output is correct
87 Correct 475 ms 58040 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 496 ms 58820 KB Output is correct
90 Correct 423 ms 59340 KB Output is correct
91 Correct 390 ms 60176 KB Output is correct