답안 #838856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838856 2023-08-27T23:41:41 Z taher 메기 농장 (IOI22_fish) C++17
100 / 100
892 ms 193104 KB
#include <bits/stdc++.h>
#include "fish.h"

using namespace std;

long long dp[700000][3][2];
bool vis[700000][3][2];

long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
  vector<vector<int>> at(n);
  vector<vector<int>> ids(n);
  for (int i = 0; i < m; i++) {
    at[x[i]].push_back(y[i]); 
    ids[x[i]].push_back(i);
  }
  
  
  for (int i = 0; i < n; i++) {
    if (at[i].empty()) {
      continue;
    }
    sort(ids[i].begin(), ids[i].end(), [&](int a, int b) {
      return y[a] < y[b];    
    });
    sort(at[i].begin(), at[i].end()); 
  }
  
  vector<vector<long long>> pref(n);
  for (int i = 0; i < n; i++) {
    if (at[i].empty()) {
      continue; 
    }
    pref[i].resize((int) at[i].size());
    pref[i][0] = 1LL * w[ids[i][0]];
    for (int j = 1; j < (int) at[i].size(); j++) {
      pref[i][j] = pref[i][j - 1] + w[ids[i][j]];
    }
  }
  
  vector<vector<int>> endpoints(n);
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      for (auto v : at[i - 1]) {
        endpoints[i].push_back(v); 
      } 
    }
    if (i < n - 1) {
      for (auto v : at[i + 1]) {
        endpoints[i].push_back(v); 
      }            
    }
    endpoints[i].push_back(-1);
    
    sort(endpoints[i].begin(), endpoints[i].end());
    endpoints[i].erase(unique(endpoints[i].begin(), endpoints[i].end()), endpoints[i].end());
  }
  
  int current = 0;
  vector<vector<int>> coords(n);
  for (int i = 0; i < n; i++) {
    coords[i].resize((int) endpoints[i].size());
    for (int j = 0; j < (int) endpoints[i].size(); j++) {
      coords[i][j] = current++; 
    }
  }  
  
  auto Count = [&](int col, int from, int to) {
    int xx = upper_bound(at[col].begin(), at[col].end(), from) - at[col].begin();
    int yy = upper_bound(at[col].begin(), at[col].end(), to) - at[col].begin();
    yy--;
    
    if (xx <= yy) {
      assert(yy < (int) at[col].size());
      long long s = pref[col][yy];
      if (xx > 0) {
        s -= pref[col][xx - 1]; 
      }
      return s;        
    }
    return 0LL;
  };
  
  /*
    0: undef
    1: increasing
    2: decreasing
  */
  
  // vector<vector<vector<long long>>> dp(current, vector<vector<long long>> (3, vector<long long> (2, -1)));
  
  function<long long(int, int, int, int)> DP = [&](int col, int id, int state, int used_dec) {
    
    if (vis[coords[col][id]][state][used_dec]) {
      return dp[coords[col][id]][state][used_dec]; 
    }
    
    long long ans = 0;
    int current_height = endpoints[col][id];
    if (state == 1) {
      if (id + 1 < (int) endpoints[col].size()) {
        long long cnt = Count(col - 1, current_height, endpoints[col][id + 1]);
        ans = max(ans, DP(col, id + 1, 1, used_dec) + cnt); 
      }      
    } else if (state == 2) {
      if (id - 1 > 0) {
        long long cnt = Count(col, endpoints[col][id - 1], current_height);
        ans = max(ans, DP(col, id - 1, 2, used_dec) + cnt);
      } 
    } else {
      if (id + 1 < (int) endpoints[col].size()) {
        ans = max(ans, DP(col, id + 1, 0, used_dec)); 
      }
    }
    
    if (col + 1 < n) {
      if (!used_dec) {
        int nid = lower_bound(endpoints[col + 1].begin(), endpoints[col + 1].end(), current_height) - endpoints[col + 1].begin();
        if (nid < (int) endpoints[col + 1].size()) {
          ans = max(ans, DP(col + 1, nid, 1, used_dec) + Count(col, current_height, endpoints[col + 1][nid])); 
        }
      }
      int nid = upper_bound(endpoints[col + 1].begin(), endpoints[col + 1].end(), current_height) - endpoints[col + 1].begin();
      --nid;
      if (nid > 0) {
        ans = max(ans, DP(col + 1, nid, 2, 1) + Count(col + 1, endpoints[col + 1][nid], current_height));
      }
      
      ans = max(ans, Count(col + 1, -1, current_height));
    }
    if (col + 2 < n) {
      int nid = lower_bound(endpoints[col + 2].begin(), endpoints[col + 2].end(), current_height) - endpoints[col + 2].begin();
      if (nid < (int) endpoints[col + 2].size()) {
        ans = max(ans, DP(col + 2, nid, 1, 0) + Count(col + 1, -1, endpoints[col + 2][nid]));
      }
      nid = 0;
      ans = max(ans, DP(col + 2, nid, 0, 0) + Count(col + 1, -1, current_height));
    }
    vis[coords[col][id]][state][used_dec] = true;
    return dp[coords[col][id]][state][used_dec] = ans;
  };
  
  long long res = DP(0, 0, 0, 0);
  return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 72080 KB Output is correct
2 Correct 113 ms 83616 KB Output is correct
3 Correct 42 ms 48588 KB Output is correct
4 Correct 38 ms 48596 KB Output is correct
5 Correct 465 ms 193104 KB Output is correct
6 Correct 351 ms 139048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 230 ms 85168 KB Output is correct
3 Correct 266 ms 99740 KB Output is correct
4 Correct 91 ms 72108 KB Output is correct
5 Correct 108 ms 83612 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 37 ms 48572 KB Output is correct
11 Correct 37 ms 48588 KB Output is correct
12 Correct 121 ms 77096 KB Output is correct
13 Correct 141 ms 89792 KB Output is correct
14 Correct 114 ms 84684 KB Output is correct
15 Correct 145 ms 83012 KB Output is correct
16 Correct 114 ms 74688 KB Output is correct
17 Correct 123 ms 82832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 48588 KB Output is correct
2 Correct 36 ms 48588 KB Output is correct
3 Correct 83 ms 52812 KB Output is correct
4 Correct 73 ms 53912 KB Output is correct
5 Correct 129 ms 65572 KB Output is correct
6 Correct 130 ms 65640 KB Output is correct
7 Correct 131 ms 65612 KB Output is correct
8 Correct 132 ms 65692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 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 468 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 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 468 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 82 ms 7244 KB Output is correct
18 Correct 81 ms 6652 KB Output is correct
19 Correct 66 ms 7408 KB Output is correct
20 Correct 58 ms 5964 KB Output is correct
21 Correct 63 ms 5708 KB Output is correct
22 Correct 145 ms 11012 KB Output is correct
23 Correct 19 ms 3540 KB Output is correct
24 Correct 64 ms 8356 KB Output is correct
25 Correct 3 ms 848 KB Output is correct
26 Correct 17 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 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 468 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 82 ms 7244 KB Output is correct
18 Correct 81 ms 6652 KB Output is correct
19 Correct 66 ms 7408 KB Output is correct
20 Correct 58 ms 5964 KB Output is correct
21 Correct 63 ms 5708 KB Output is correct
22 Correct 145 ms 11012 KB Output is correct
23 Correct 19 ms 3540 KB Output is correct
24 Correct 64 ms 8356 KB Output is correct
25 Correct 3 ms 848 KB Output is correct
26 Correct 17 ms 3156 KB Output is correct
27 Correct 4 ms 2388 KB Output is correct
28 Correct 410 ms 31136 KB Output is correct
29 Correct 589 ms 67052 KB Output is correct
30 Correct 683 ms 113292 KB Output is correct
31 Correct 717 ms 111692 KB Output is correct
32 Correct 468 ms 36300 KB Output is correct
33 Correct 721 ms 116996 KB Output is correct
34 Correct 731 ms 115768 KB Output is correct
35 Correct 221 ms 34816 KB Output is correct
36 Correct 746 ms 98292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 48588 KB Output is correct
2 Correct 36 ms 48588 KB Output is correct
3 Correct 83 ms 52812 KB Output is correct
4 Correct 73 ms 53912 KB Output is correct
5 Correct 129 ms 65572 KB Output is correct
6 Correct 130 ms 65640 KB Output is correct
7 Correct 131 ms 65612 KB Output is correct
8 Correct 132 ms 65692 KB Output is correct
9 Correct 161 ms 70936 KB Output is correct
10 Correct 93 ms 37584 KB Output is correct
11 Correct 198 ms 74764 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 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 1 ms 212 KB Output is correct
18 Correct 40 ms 48588 KB Output is correct
19 Correct 39 ms 48588 KB Output is correct
20 Correct 39 ms 48604 KB Output is correct
21 Correct 38 ms 48596 KB Output is correct
22 Correct 192 ms 76304 KB Output is correct
23 Correct 276 ms 102012 KB Output is correct
24 Correct 263 ms 105940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 72080 KB Output is correct
2 Correct 113 ms 83616 KB Output is correct
3 Correct 42 ms 48588 KB Output is correct
4 Correct 38 ms 48596 KB Output is correct
5 Correct 465 ms 193104 KB Output is correct
6 Correct 351 ms 139048 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 230 ms 85168 KB Output is correct
9 Correct 266 ms 99740 KB Output is correct
10 Correct 91 ms 72108 KB Output is correct
11 Correct 108 ms 83612 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 37 ms 48572 KB Output is correct
17 Correct 37 ms 48588 KB Output is correct
18 Correct 121 ms 77096 KB Output is correct
19 Correct 141 ms 89792 KB Output is correct
20 Correct 114 ms 84684 KB Output is correct
21 Correct 145 ms 83012 KB Output is correct
22 Correct 114 ms 74688 KB Output is correct
23 Correct 123 ms 82832 KB Output is correct
24 Correct 38 ms 48588 KB Output is correct
25 Correct 36 ms 48588 KB Output is correct
26 Correct 83 ms 52812 KB Output is correct
27 Correct 73 ms 53912 KB Output is correct
28 Correct 129 ms 65572 KB Output is correct
29 Correct 130 ms 65640 KB Output is correct
30 Correct 131 ms 65612 KB Output is correct
31 Correct 132 ms 65692 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 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 1 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 468 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 724 KB Output is correct
48 Correct 82 ms 7244 KB Output is correct
49 Correct 81 ms 6652 KB Output is correct
50 Correct 66 ms 7408 KB Output is correct
51 Correct 58 ms 5964 KB Output is correct
52 Correct 63 ms 5708 KB Output is correct
53 Correct 145 ms 11012 KB Output is correct
54 Correct 19 ms 3540 KB Output is correct
55 Correct 64 ms 8356 KB Output is correct
56 Correct 3 ms 848 KB Output is correct
57 Correct 17 ms 3156 KB Output is correct
58 Correct 4 ms 2388 KB Output is correct
59 Correct 410 ms 31136 KB Output is correct
60 Correct 589 ms 67052 KB Output is correct
61 Correct 683 ms 113292 KB Output is correct
62 Correct 717 ms 111692 KB Output is correct
63 Correct 468 ms 36300 KB Output is correct
64 Correct 721 ms 116996 KB Output is correct
65 Correct 731 ms 115768 KB Output is correct
66 Correct 221 ms 34816 KB Output is correct
67 Correct 746 ms 98292 KB Output is correct
68 Correct 161 ms 70936 KB Output is correct
69 Correct 93 ms 37584 KB Output is correct
70 Correct 198 ms 74764 KB Output is correct
71 Correct 0 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 1 ms 212 KB Output is correct
77 Correct 40 ms 48588 KB Output is correct
78 Correct 39 ms 48588 KB Output is correct
79 Correct 39 ms 48604 KB Output is correct
80 Correct 38 ms 48596 KB Output is correct
81 Correct 192 ms 76304 KB Output is correct
82 Correct 276 ms 102012 KB Output is correct
83 Correct 263 ms 105940 KB Output is correct
84 Correct 868 ms 154668 KB Output is correct
85 Correct 892 ms 153136 KB Output is correct
86 Correct 540 ms 157804 KB Output is correct
87 Correct 534 ms 162764 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 525 ms 162596 KB Output is correct
90 Correct 525 ms 137372 KB Output is correct
91 Correct 385 ms 97204 KB Output is correct