답안 #626599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626599 2022-08-11T14:54:07 Z d4xn 메기 농장 (IOI22_fish) C++17
32 / 100
1000 ms 175584 KB
#pragma GCC optimize ("Ofast")
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
 
#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];
unordered_map<int, ll> dp[4][N];
 
ll mx(int curr, int h1, int state) {
  if (curr == -1) return 0;
  
  auto it = dp[state][curr].find(h1);
  if (it != dp[state][curr].end()) return it->second;
  
  if (state != 3) dp[state][curr][h1] = mx(curr-1, h1, 3);
  else dp[state][curr][h1] = mx(curr-1, 0, 2);
  it = dp[state][curr].find(h1);

  if (!state) {
    if (curr+1 < n) {
      for (int h : c[curr+1]) {
        if (h >= h1) break;

        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }
  
        // ver si he eliminado alguno de los que he pillado
        cnt -= pre[curr][min(h, h1)-1];
  
        it->second = max(it->second, cnt + mx(curr-1, h, 0));
      }
    }
  
    if (curr-1 >= 0) {
      for (int h : c[curr-1]) {
        if (h >= h1) break;

        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }
  
        // ver si he eliminado alguno de los que he pillado
        cnt -= pre[curr][min(h, h1)-1];
  
        it->second = max(it->second, cnt + mx(curr-1, h, 0));
      }
    }
  }
  else if (state == 1 || state == 2) {
    if (curr+1 < n) {
      for (int h : c[curr+1]) {
        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }

        // ver si pillo los de la derecha      
        if (h > h1 && curr+1 < n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
        }
        
        // ver si he eliminado alguno de los que he pillado
        cnt -= pre[curr][min(h, h1)-1];
  
        if (h <= h1) it->second = max(it->second, cnt + mx(curr-1, h, 0));
        else it->second = max(it->second, cnt + mx(curr-1, h, 1));
      }
    }
  
    if (curr-1 >= 0) {
      for (int h : c[curr-1]) {
        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }

        // ver si pillo los de la derecha      
        if (h > h1 && curr+1 < n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
        }
        
        // ver si he eliminado alguno de los que he pillado
        cnt -= pre[curr][min(h, h1)-1];
  
        if (h <= h1) it->second = max(it->second, cnt + mx(curr-1, h, 0));
        else it->second = max(it->second, cnt + mx(curr-1, h, 1));
      }
    }
  }
  else {
    if (curr+1 < n) {
      for (int h : c[curr+1]) {
        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }

        // ver si pillo los de la derecha      
        if (h > h1 && curr+1 < n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
        }
  
        it->second = max(it->second, cnt + mx(curr-1, h, 2));
      }
    }
  
    if (curr-1 >= 0) {
      for (int h : c[curr-1]) {
        h++;
        ll cnt = 0;
  
        // ver si pillo los de la izquierda      
        if (curr-1 >= 0) {
          cnt += pre[curr-1][h-1];
        }

        // ver si pillo los de la derecha      
        if (h > h1 && curr+1 < n) {
          cnt += pre[curr+1][h-1] - pre[curr+1][h1-1];
        }
  
        it->second = max(it->second, cnt + mx(curr-1, h, 2));
      }
    }
  }

  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, 2);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 40824 KB Output is correct
2 Correct 63 ms 42784 KB Output is correct
3 Correct 21 ms 35416 KB Output is correct
4 Correct 21 ms 35524 KB Output is correct
5 Correct 169 ms 57568 KB Output is correct
6 Correct 251 ms 59788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 35412 KB Output is correct
2 Correct 87 ms 47736 KB Output is correct
3 Correct 105 ms 51460 KB Output is correct
4 Correct 51 ms 40836 KB Output is correct
5 Correct 59 ms 42848 KB Output is correct
6 Correct 21 ms 35412 KB Output is correct
7 Correct 21 ms 35412 KB Output is correct
8 Correct 21 ms 35404 KB Output is correct
9 Correct 21 ms 35412 KB Output is correct
10 Correct 21 ms 35420 KB Output is correct
11 Correct 23 ms 35524 KB Output is correct
12 Correct 56 ms 42276 KB Output is correct
13 Correct 64 ms 44224 KB Output is correct
14 Correct 54 ms 42296 KB Output is correct
15 Correct 65 ms 43644 KB Output is correct
16 Correct 56 ms 42360 KB Output is correct
17 Correct 58 ms 43608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 35476 KB Output is correct
2 Correct 67 ms 79244 KB Output is correct
3 Correct 204 ms 119656 KB Output is correct
4 Correct 178 ms 113656 KB Output is correct
5 Correct 313 ms 150704 KB Output is correct
6 Correct 307 ms 150568 KB Output is correct
7 Correct 303 ms 150492 KB Output is correct
8 Correct 322 ms 150524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 35412 KB Output is correct
2 Correct 21 ms 35412 KB Output is correct
3 Correct 22 ms 35500 KB Output is correct
4 Correct 22 ms 35520 KB Output is correct
5 Correct 23 ms 35472 KB Output is correct
6 Correct 25 ms 35440 KB Output is correct
7 Correct 21 ms 35528 KB Output is correct
8 Correct 24 ms 35412 KB Output is correct
9 Correct 24 ms 35688 KB Output is correct
10 Correct 26 ms 36308 KB Output is correct
11 Correct 22 ms 35856 KB Output is correct
12 Correct 24 ms 36052 KB Output is correct
13 Correct 23 ms 35632 KB Output is correct
14 Correct 22 ms 35944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 35412 KB Output is correct
2 Correct 21 ms 35412 KB Output is correct
3 Correct 22 ms 35500 KB Output is correct
4 Correct 22 ms 35520 KB Output is correct
5 Correct 23 ms 35472 KB Output is correct
6 Correct 25 ms 35440 KB Output is correct
7 Correct 21 ms 35528 KB Output is correct
8 Correct 24 ms 35412 KB Output is correct
9 Correct 24 ms 35688 KB Output is correct
10 Correct 26 ms 36308 KB Output is correct
11 Correct 22 ms 35856 KB Output is correct
12 Correct 24 ms 36052 KB Output is correct
13 Correct 23 ms 35632 KB Output is correct
14 Correct 22 ms 35944 KB Output is correct
15 Correct 22 ms 35904 KB Output is correct
16 Correct 42 ms 36280 KB Output is correct
17 Execution timed out 1092 ms 48576 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 35412 KB Output is correct
2 Correct 21 ms 35412 KB Output is correct
3 Correct 22 ms 35500 KB Output is correct
4 Correct 22 ms 35520 KB Output is correct
5 Correct 23 ms 35472 KB Output is correct
6 Correct 25 ms 35440 KB Output is correct
7 Correct 21 ms 35528 KB Output is correct
8 Correct 24 ms 35412 KB Output is correct
9 Correct 24 ms 35688 KB Output is correct
10 Correct 26 ms 36308 KB Output is correct
11 Correct 22 ms 35856 KB Output is correct
12 Correct 24 ms 36052 KB Output is correct
13 Correct 23 ms 35632 KB Output is correct
14 Correct 22 ms 35944 KB Output is correct
15 Correct 22 ms 35904 KB Output is correct
16 Correct 42 ms 36280 KB Output is correct
17 Execution timed out 1092 ms 48576 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 35476 KB Output is correct
2 Correct 67 ms 79244 KB Output is correct
3 Correct 204 ms 119656 KB Output is correct
4 Correct 178 ms 113656 KB Output is correct
5 Correct 313 ms 150704 KB Output is correct
6 Correct 307 ms 150568 KB Output is correct
7 Correct 303 ms 150492 KB Output is correct
8 Correct 322 ms 150524 KB Output is correct
9 Correct 426 ms 175584 KB Output is correct
10 Correct 260 ms 103556 KB Output is correct
11 Correct 517 ms 171696 KB Output is correct
12 Correct 20 ms 35488 KB Output is correct
13 Correct 21 ms 35472 KB Output is correct
14 Correct 21 ms 35452 KB Output is correct
15 Correct 23 ms 35468 KB Output is correct
16 Correct 23 ms 35528 KB Output is correct
17 Correct 22 ms 35432 KB Output is correct
18 Correct 21 ms 35452 KB Output is correct
19 Correct 21 ms 35412 KB Output is correct
20 Correct 78 ms 79256 KB Output is correct
21 Correct 84 ms 79292 KB Output is correct
22 Incorrect 436 ms 163040 KB 1st lines differ - on the 1st token, expected: '45561826463480', found: '45553442149303'
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 40824 KB Output is correct
2 Correct 63 ms 42784 KB Output is correct
3 Correct 21 ms 35416 KB Output is correct
4 Correct 21 ms 35524 KB Output is correct
5 Correct 169 ms 57568 KB Output is correct
6 Correct 251 ms 59788 KB Output is correct
7 Correct 22 ms 35412 KB Output is correct
8 Correct 87 ms 47736 KB Output is correct
9 Correct 105 ms 51460 KB Output is correct
10 Correct 51 ms 40836 KB Output is correct
11 Correct 59 ms 42848 KB Output is correct
12 Correct 21 ms 35412 KB Output is correct
13 Correct 21 ms 35412 KB Output is correct
14 Correct 21 ms 35404 KB Output is correct
15 Correct 21 ms 35412 KB Output is correct
16 Correct 21 ms 35420 KB Output is correct
17 Correct 23 ms 35524 KB Output is correct
18 Correct 56 ms 42276 KB Output is correct
19 Correct 64 ms 44224 KB Output is correct
20 Correct 54 ms 42296 KB Output is correct
21 Correct 65 ms 43644 KB Output is correct
22 Correct 56 ms 42360 KB Output is correct
23 Correct 58 ms 43608 KB Output is correct
24 Correct 21 ms 35476 KB Output is correct
25 Correct 67 ms 79244 KB Output is correct
26 Correct 204 ms 119656 KB Output is correct
27 Correct 178 ms 113656 KB Output is correct
28 Correct 313 ms 150704 KB Output is correct
29 Correct 307 ms 150568 KB Output is correct
30 Correct 303 ms 150492 KB Output is correct
31 Correct 322 ms 150524 KB Output is correct
32 Correct 22 ms 35412 KB Output is correct
33 Correct 21 ms 35412 KB Output is correct
34 Correct 22 ms 35500 KB Output is correct
35 Correct 22 ms 35520 KB Output is correct
36 Correct 23 ms 35472 KB Output is correct
37 Correct 25 ms 35440 KB Output is correct
38 Correct 21 ms 35528 KB Output is correct
39 Correct 24 ms 35412 KB Output is correct
40 Correct 24 ms 35688 KB Output is correct
41 Correct 26 ms 36308 KB Output is correct
42 Correct 22 ms 35856 KB Output is correct
43 Correct 24 ms 36052 KB Output is correct
44 Correct 23 ms 35632 KB Output is correct
45 Correct 22 ms 35944 KB Output is correct
46 Correct 22 ms 35904 KB Output is correct
47 Correct 42 ms 36280 KB Output is correct
48 Execution timed out 1092 ms 48576 KB Time limit exceeded
49 Halted 0 ms 0 KB -