Submission #625790

# Submission time Handle Problem Language Result Execution time Memory
625790 2022-08-10T19:17:32 Z d4xn Catfish Farm (IOI22_fish) C++17
46 / 100
1000 ms 231632 KB
#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx2")
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
 
//#define int long long
#define ll long long
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
#define tuple2 tuple<int, int>
#define tuple3 tuple<int, int, int>
 
const int N = 1e5;
 
int n, m;
vi c[N];
unordered_map<int, int> w[N];
unordered_map<int, ll> pre[N];
map<tuple2, ll> dp[N];
//map<tuple<int, int, int>, ll> dp; // dp[i][j][k] = maximos peces que puedo conseguir
              // con las k primeras columnas donde
              // en la columna k+1 he subido i-1
              // en la columna k+2 he subido j-1
 
ll mx(int curr, int h1, int h2) {
  if (curr == -1) return 0;
  
  tuple2 t = make_tuple(h1, h2);
  auto it = dp[curr].find(t);
  if (it != dp[curr].end()) return it->second;
  
  dp[curr][t] = mx(curr-1, 0, h1);
  it = dp[curr].find(t);
 
  if (curr+1 < n) {
    for (int h : c[curr+1]) {
      h++;
      ll cnt = 0;
 
      // ver si pillo los de la derecha
      if (curr+1 < n && h > max(h1, h2)) {
        if (curr+2 == n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
          //assert(pre[curr+1].count(h-1) && pre[curr+1].count(h1-1));
        }
        else {
          cnt += pre[curr+1][h-1] - pre[curr+1][max(h1, h2)-1];
          //assert(pre[curr+1].count(h-1) && pre[curr+1].count(max(h1, h2)-1));
        }
      }
 
      // ver si pillo los de la izquierda      
      if (curr-1 >= 0) {
        cnt += pre[curr-1][h-1];
        //assert(pre[curr-1].count(h-1));
      }
      
 
      // ver si he eliminado alguno de los que he pillado
      cnt -= pre[curr][min(h, h1)-1];
      //assert(pre[curr].count(min(h, h1)-1));
 
      it->second = max(it->second, cnt + mx(curr-1, h, h1));
    }
  }
 
  if (curr-1 >= 0) {
    for (int h : c[curr-1]) {
      h++;
      ll cnt = 0;
 
      // ver si pillo los de la derecha
      if (curr+1 < n && h > max(h1, h2)) {
        if (curr+2 == n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
          //assert(pre[curr+1].count(h-1) && pre[curr+1].count(h1-1));
        }
        else {
          cnt += pre[curr+1][h-1] - pre[curr+1][max(h1, h2)-1];
          //assert(pre[curr+1].count(h-1) && pre[curr+1].count(max(h1, h2)-1));
        }
      }
 
      // ver si pillo los de la izquierda      
      if (curr-1 >= 0) {
        cnt += pre[curr-1][h-1];
        //assert(pre[curr-1].count(h-1));
      }
      
 
      // ver si he eliminado alguno de los que he pillado
      cnt -= pre[curr][min(h, h1)-1];
      //assert(pre[curr].count(min(h, h1)-1));
 
      it->second = max(it->second, cnt + mx(curr-1, h, h1));
    }
  }

  return it->second;
}
 
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
  n = N;
  m = M;
 
  ll sum = 0;
  bool odd = 0;
  int mxX = 0;
  for (int i = 0; i < m; i++) {
    sum += W[i];
    if (X[i]%2) odd = 1;
    mxX = max(mxX, X[i]);

    w[X[i]][Y[i]] = W[i];
    c[X[i]].push_back(Y[i]);
  }
 
  if (!odd) return sum;
  if (mxX <= 1) {
    ll pre0[n], pre1[n];
    for (int i = 0; i < n; i++) {
      pre0[i] = (i ? pre0[i-1] : 0);
      pre1[i] = (i ? pre1[i-1] : 0);

      auto it = w[0].find(i);
      if (it != w[0].end()) pre0[i] += it->second;


      it = w[1].find(i);
      if (it != w[1].end()) pre1[i] += it->second;
    }

    if (n == 2) return max(pre0[n-1], pre1[n-1]);

    ll ans = max(pre0[n-1], pre1[n-1]);
    for (int i = 0; i < n; i++) {
      ans = max(ans, pre0[i] + pre1[n-1] - pre1[i]);
    }

    if (n >= 3) return ans;
  }

  map<int, int> heights;
  for (int i = 0; i < n; i++) {
    pre[i][-1] = 0;

    for (int &h : c[i]) heights[h]++;
 
    if (i >= 3) {
      for (int &h : c[i-3]) {
        heights[h]--;
        if (!heights[h]) heights.erase(h);
      }
    }
 
    ll sum1 = 0;
    ll sum2 = 0;
    ll sum3 = 0;
    for (auto &[h, cnt] : heights) {
      auto it = w[i].find(h);
      if (it != w[i].end()) sum3 += it->second;
      pre[i][h] = sum3;
 
      if (i-1 >= 0) {
        it = w[i-1].find(h);
        if (it != w[i-1].end()) sum2 += it->second;
        pre[i-1][h] = sum2;
      }
      if (i-2 >= 0) {
        it = w[i-2].find(h);
        if (it != w[i-2].end()) sum1 += it->second;
        pre[i-2][h] = sum1;
      }
    }
  }

  return mx(n-1, 0, 0);
}
# Verdict Execution time Memory Grader output
1 Correct 39 ms 23604 KB Output is correct
2 Correct 48 ms 25676 KB Output is correct
3 Correct 10 ms 18260 KB Output is correct
4 Correct 12 ms 18260 KB Output is correct
5 Correct 161 ms 40344 KB Output is correct
6 Correct 240 ms 42560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 18260 KB Output is correct
2 Correct 72 ms 30456 KB Output is correct
3 Correct 103 ms 34308 KB Output is correct
4 Correct 40 ms 23612 KB Output is correct
5 Correct 49 ms 25640 KB Output is correct
6 Correct 10 ms 18260 KB Output is correct
7 Correct 10 ms 18252 KB Output is correct
8 Correct 11 ms 18260 KB Output is correct
9 Correct 13 ms 18292 KB Output is correct
10 Correct 11 ms 18260 KB Output is correct
11 Correct 10 ms 18304 KB Output is correct
12 Correct 43 ms 25052 KB Output is correct
13 Correct 50 ms 27012 KB Output is correct
14 Correct 44 ms 25020 KB Output is correct
15 Correct 50 ms 26424 KB Output is correct
16 Correct 43 ms 25052 KB Output is correct
17 Correct 48 ms 26372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18304 KB Output is correct
2 Correct 47 ms 52680 KB Output is correct
3 Correct 126 ms 72448 KB Output is correct
4 Correct 101 ms 68540 KB Output is correct
5 Correct 194 ms 94112 KB Output is correct
6 Correct 197 ms 94220 KB Output is correct
7 Correct 207 ms 94220 KB Output is correct
8 Correct 206 ms 94192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 18260 KB Output is correct
2 Correct 13 ms 18260 KB Output is correct
3 Correct 10 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 11 ms 18304 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 12 ms 18260 KB Output is correct
8 Correct 10 ms 18260 KB Output is correct
9 Correct 12 ms 18624 KB Output is correct
10 Correct 37 ms 20096 KB Output is correct
11 Correct 18 ms 19028 KB Output is correct
12 Correct 20 ms 19420 KB Output is correct
13 Correct 11 ms 18416 KB Output is correct
14 Correct 16 ms 18956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 18260 KB Output is correct
2 Correct 13 ms 18260 KB Output is correct
3 Correct 10 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 11 ms 18304 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 12 ms 18260 KB Output is correct
8 Correct 10 ms 18260 KB Output is correct
9 Correct 12 ms 18624 KB Output is correct
10 Correct 37 ms 20096 KB Output is correct
11 Correct 18 ms 19028 KB Output is correct
12 Correct 20 ms 19420 KB Output is correct
13 Correct 11 ms 18416 KB Output is correct
14 Correct 16 ms 18956 KB Output is correct
15 Correct 11 ms 18544 KB Output is correct
16 Execution timed out 1086 ms 26572 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 18260 KB Output is correct
2 Correct 13 ms 18260 KB Output is correct
3 Correct 10 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 11 ms 18304 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 12 ms 18260 KB Output is correct
8 Correct 10 ms 18260 KB Output is correct
9 Correct 12 ms 18624 KB Output is correct
10 Correct 37 ms 20096 KB Output is correct
11 Correct 18 ms 19028 KB Output is correct
12 Correct 20 ms 19420 KB Output is correct
13 Correct 11 ms 18416 KB Output is correct
14 Correct 16 ms 18956 KB Output is correct
15 Correct 11 ms 18544 KB Output is correct
16 Execution timed out 1086 ms 26572 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18304 KB Output is correct
2 Correct 47 ms 52680 KB Output is correct
3 Correct 126 ms 72448 KB Output is correct
4 Correct 101 ms 68540 KB Output is correct
5 Correct 194 ms 94112 KB Output is correct
6 Correct 197 ms 94220 KB Output is correct
7 Correct 207 ms 94220 KB Output is correct
8 Correct 206 ms 94192 KB Output is correct
9 Correct 336 ms 138056 KB Output is correct
10 Correct 227 ms 76144 KB Output is correct
11 Correct 427 ms 134072 KB Output is correct
12 Correct 10 ms 18260 KB Output is correct
13 Correct 10 ms 18260 KB Output is correct
14 Correct 15 ms 18260 KB Output is correct
15 Correct 10 ms 18260 KB Output is correct
16 Correct 10 ms 18260 KB Output is correct
17 Correct 11 ms 18260 KB Output is correct
18 Correct 10 ms 18232 KB Output is correct
19 Correct 13 ms 18304 KB Output is correct
20 Correct 53 ms 52640 KB Output is correct
21 Correct 47 ms 52668 KB Output is correct
22 Correct 443 ms 133024 KB Output is correct
23 Correct 918 ms 210248 KB Output is correct
24 Correct 921 ms 231632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 23604 KB Output is correct
2 Correct 48 ms 25676 KB Output is correct
3 Correct 10 ms 18260 KB Output is correct
4 Correct 12 ms 18260 KB Output is correct
5 Correct 161 ms 40344 KB Output is correct
6 Correct 240 ms 42560 KB Output is correct
7 Correct 11 ms 18260 KB Output is correct
8 Correct 72 ms 30456 KB Output is correct
9 Correct 103 ms 34308 KB Output is correct
10 Correct 40 ms 23612 KB Output is correct
11 Correct 49 ms 25640 KB Output is correct
12 Correct 10 ms 18260 KB Output is correct
13 Correct 10 ms 18252 KB Output is correct
14 Correct 11 ms 18260 KB Output is correct
15 Correct 13 ms 18292 KB Output is correct
16 Correct 11 ms 18260 KB Output is correct
17 Correct 10 ms 18304 KB Output is correct
18 Correct 43 ms 25052 KB Output is correct
19 Correct 50 ms 27012 KB Output is correct
20 Correct 44 ms 25020 KB Output is correct
21 Correct 50 ms 26424 KB Output is correct
22 Correct 43 ms 25052 KB Output is correct
23 Correct 48 ms 26372 KB Output is correct
24 Correct 10 ms 18304 KB Output is correct
25 Correct 47 ms 52680 KB Output is correct
26 Correct 126 ms 72448 KB Output is correct
27 Correct 101 ms 68540 KB Output is correct
28 Correct 194 ms 94112 KB Output is correct
29 Correct 197 ms 94220 KB Output is correct
30 Correct 207 ms 94220 KB Output is correct
31 Correct 206 ms 94192 KB Output is correct
32 Correct 11 ms 18260 KB Output is correct
33 Correct 13 ms 18260 KB Output is correct
34 Correct 10 ms 18260 KB Output is correct
35 Correct 11 ms 18260 KB Output is correct
36 Correct 11 ms 18304 KB Output is correct
37 Correct 11 ms 18260 KB Output is correct
38 Correct 12 ms 18260 KB Output is correct
39 Correct 10 ms 18260 KB Output is correct
40 Correct 12 ms 18624 KB Output is correct
41 Correct 37 ms 20096 KB Output is correct
42 Correct 18 ms 19028 KB Output is correct
43 Correct 20 ms 19420 KB Output is correct
44 Correct 11 ms 18416 KB Output is correct
45 Correct 16 ms 18956 KB Output is correct
46 Correct 11 ms 18544 KB Output is correct
47 Execution timed out 1086 ms 26572 KB Time limit exceeded
48 Halted 0 ms 0 KB -