답안 #749588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
749588 2023-05-28T09:28:15 Z farhan132 메기 농장 (IOI22_fish) C++17
100 / 100
903 ms 58956 KB
#include "fish.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll,  ll, ll > tp;

#define ff first
#define ss second
#define pb push_back
#define in insert

const ll INF = (ll)1e18;

#define mem(a , b) memset(a, b ,sizeof(a))

const ll N = 1e5 + 5;

ll dp[2][2][2][N], pref[2][2][2][N], suf[2][2][2][N];
ll n;
vector < ii > v[N];
vector < ll > sum[N];

long long max_weights(int _n, int m, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
  n = _n;
  mem(dp, 0); mem(pref, 0);
  for(ll i = 0; i < m; i++){
    v[X[i]].pb({Y[i], 0});
    v[X[i]].pb({Y[i] + 1, W[i]});
  }
  for(ll i = 0; i < n; i++){
    v[i].pb({n, 0}); v[i].pb({0, 0});
    sort(v[i].begin(), v[i].end());
    vector < ii > f;
    for(ll j = 0; j < v[i].size(); j++){
        if(!f.size()){
            f.pb(v[i][j]);
        }else{
            if(f.back().ff == v[i][j].ff){
                f.pop_back();
            }
            f.pb(v[i][j]);
        }
    }
    v[i] = f;
    ll S = 0;
    for(ll j = 0; j < v[i].size(); j++){
        S += v[i][j].ss; sum[i].pb(S);
    }
  }
  auto get = [&](ll i, ll k){
    auto it = upper_bound(v[i].begin(), v[i].end(), make_pair(k+1, -INF)) - v[i].begin() - 1;
    if(it < 0) it = 0;
    return sum[i][it];
  };
  ll row = 0;

  ll ans = 0;

  for(ll i = 0; i < n; i++){
    row ^= 1; 
    // 0 -> i-1 > i, i > i+1
    // 1 -> i-1 < i, i < i+1

    // 0 -> sz[i-1] < sz[i], sz[i] < sz[i+1]
    // 1 -> sz[i-1] > sz[i], sz[i] > sz[i+1] 

    ll sz = v[i].size();

    for(ll j = 0; j <= sz; j++){
        for(ll c = 0; c < 2; c++)
            dp[row][c][0][j] = dp[row][c][1][j] = -INF; 
    }
    for(ll l = 0; l < 2; l++){
        for(ll d1 = 0; d1 < 2; d1++){
            for(ll r = 0; r < 2; r++){
                for(ll d2 = 0; d2 < 2; d2++){
                    if((!l && d1) && (r && !d2)) continue;
                    for(ll j = 0; j < sz; j++){
                        ll V = 0;
                        ll J = 0;
                        if(i > 0){
                            auto it = upper_bound(v[i-1].begin(), v[i-1].end(), make_pair(v[i][j].ff, -INF)) - v[i-1].begin();
                            if(it == v[i-1].size()) it--;
                            J = it;
                        }
                        if(l == 0 && d1){
                            V = suf[1-row][l][d1][J] - get(i, v[i][j].ff);
                        }else if(l == 0){
                            V = pref[1-row][l][d1][J-1];
                        }
                        if(l == 1 && !d1){
                            V = pref[1-row][l][d1][J-1] + (i > 0 ? get(i - 1, v[i][j].ff) : 0);
                        }else if(l == 1){
                            V = suf[1-row][l][d1][J];
                        }
                        if(r == 0 && d2 && i + 1 < n){
                            V += get(i + 1, v[i][j].ff);
                        }
                        if(r == 1 && !d2){
                            V -= get(i, v[i][j].ff);
                        }
                        dp[row][r][d2][j] = max(dp[row][r][d2][j], V);
                        ans = max(ans, V);
                    }
                }
            }
        }
    }
    // cout << i << ' ' << ans << '\n';
    for(ll c = 0; c < 2; c++){
        for(ll d = 0; d < 2; d++){
            suf[row][c][d][sz] = -INF;
            for(ll j = sz; j >= 0; j--){
                suf[row][c][d][j] = max(suf[row][c][d][j + 1], dp[row][c][d][j]);
            }
            for(ll j = 0; j < sz; j++){
                pref[row][c][d][j] = max(dp[row][c][d][j], (j > 0 ? pref[row][c][d][j - 1]: -INF));
            }
        }
    }
  }

  return ans;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:39:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for(ll j = 0; j < v[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~
fish.cpp:51:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(ll j = 0; j < v[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~
fish.cpp:88:35: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |                             if(it == v[i-1].size()) it--;
      |                                ~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 32760 KB Output is correct
2 Correct 188 ms 36452 KB Output is correct
3 Correct 86 ms 25384 KB Output is correct
4 Correct 85 ms 25284 KB Output is correct
5 Correct 384 ms 57464 KB Output is correct
6 Correct 466 ms 58956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17492 KB Output is correct
2 Correct 338 ms 41304 KB Output is correct
3 Correct 363 ms 47784 KB Output is correct
4 Correct 160 ms 34112 KB Output is correct
5 Correct 175 ms 36468 KB Output is correct
6 Correct 9 ms 17576 KB Output is correct
7 Correct 8 ms 17492 KB Output is correct
8 Correct 8 ms 17492 KB Output is correct
9 Correct 9 ms 17492 KB Output is correct
10 Correct 86 ms 25376 KB Output is correct
11 Correct 88 ms 25432 KB Output is correct
12 Correct 161 ms 33964 KB Output is correct
13 Correct 182 ms 36520 KB Output is correct
14 Correct 165 ms 34132 KB Output is correct
15 Correct 219 ms 35872 KB Output is correct
16 Correct 166 ms 34036 KB Output is correct
17 Correct 184 ms 35888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 25292 KB Output is correct
2 Correct 87 ms 25344 KB Output is correct
3 Correct 127 ms 29324 KB Output is correct
4 Correct 112 ms 28568 KB Output is correct
5 Correct 159 ms 33944 KB Output is correct
6 Correct 153 ms 33296 KB Output is correct
7 Correct 152 ms 33992 KB Output is correct
8 Correct 151 ms 33984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17552 KB Output is correct
2 Correct 9 ms 17552 KB Output is correct
3 Correct 8 ms 17492 KB Output is correct
4 Correct 8 ms 17492 KB Output is correct
5 Correct 8 ms 17548 KB Output is correct
6 Correct 9 ms 17560 KB Output is correct
7 Correct 9 ms 17548 KB Output is correct
8 Correct 8 ms 17492 KB Output is correct
9 Correct 9 ms 17564 KB Output is correct
10 Correct 10 ms 17876 KB Output is correct
11 Correct 9 ms 17556 KB Output is correct
12 Correct 10 ms 17692 KB Output is correct
13 Correct 9 ms 17552 KB Output is correct
14 Correct 9 ms 17564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17552 KB Output is correct
2 Correct 9 ms 17552 KB Output is correct
3 Correct 8 ms 17492 KB Output is correct
4 Correct 8 ms 17492 KB Output is correct
5 Correct 8 ms 17548 KB Output is correct
6 Correct 9 ms 17560 KB Output is correct
7 Correct 9 ms 17548 KB Output is correct
8 Correct 8 ms 17492 KB Output is correct
9 Correct 9 ms 17564 KB Output is correct
10 Correct 10 ms 17876 KB Output is correct
11 Correct 9 ms 17556 KB Output is correct
12 Correct 10 ms 17692 KB Output is correct
13 Correct 9 ms 17552 KB Output is correct
14 Correct 9 ms 17564 KB Output is correct
15 Correct 9 ms 17620 KB Output is correct
16 Correct 11 ms 17748 KB Output is correct
17 Correct 91 ms 21068 KB Output is correct
18 Correct 86 ms 21836 KB Output is correct
19 Correct 69 ms 21836 KB Output is correct
20 Correct 70 ms 21912 KB Output is correct
21 Correct 76 ms 21836 KB Output is correct
22 Correct 149 ms 25936 KB Output is correct
23 Correct 31 ms 18548 KB Output is correct
24 Correct 79 ms 20256 KB Output is correct
25 Correct 12 ms 17620 KB Output is correct
26 Correct 27 ms 18436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17552 KB Output is correct
2 Correct 9 ms 17552 KB Output is correct
3 Correct 8 ms 17492 KB Output is correct
4 Correct 8 ms 17492 KB Output is correct
5 Correct 8 ms 17548 KB Output is correct
6 Correct 9 ms 17560 KB Output is correct
7 Correct 9 ms 17548 KB Output is correct
8 Correct 8 ms 17492 KB Output is correct
9 Correct 9 ms 17564 KB Output is correct
10 Correct 10 ms 17876 KB Output is correct
11 Correct 9 ms 17556 KB Output is correct
12 Correct 10 ms 17692 KB Output is correct
13 Correct 9 ms 17552 KB Output is correct
14 Correct 9 ms 17564 KB Output is correct
15 Correct 9 ms 17620 KB Output is correct
16 Correct 11 ms 17748 KB Output is correct
17 Correct 91 ms 21068 KB Output is correct
18 Correct 86 ms 21836 KB Output is correct
19 Correct 69 ms 21836 KB Output is correct
20 Correct 70 ms 21912 KB Output is correct
21 Correct 76 ms 21836 KB Output is correct
22 Correct 149 ms 25936 KB Output is correct
23 Correct 31 ms 18548 KB Output is correct
24 Correct 79 ms 20256 KB Output is correct
25 Correct 12 ms 17620 KB Output is correct
26 Correct 27 ms 18436 KB Output is correct
27 Correct 12 ms 18004 KB Output is correct
28 Correct 419 ms 35864 KB Output is correct
29 Correct 581 ms 42448 KB Output is correct
30 Correct 744 ms 43492 KB Output is correct
31 Correct 711 ms 43356 KB Output is correct
32 Correct 502 ms 44172 KB Output is correct
33 Correct 742 ms 43392 KB Output is correct
34 Correct 717 ms 43352 KB Output is correct
35 Correct 277 ms 28620 KB Output is correct
36 Correct 762 ms 44000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 25292 KB Output is correct
2 Correct 87 ms 25344 KB Output is correct
3 Correct 127 ms 29324 KB Output is correct
4 Correct 112 ms 28568 KB Output is correct
5 Correct 159 ms 33944 KB Output is correct
6 Correct 153 ms 33296 KB Output is correct
7 Correct 152 ms 33992 KB Output is correct
8 Correct 151 ms 33984 KB Output is correct
9 Correct 184 ms 33692 KB Output is correct
10 Correct 130 ms 32228 KB Output is correct
11 Correct 257 ms 46884 KB Output is correct
12 Correct 8 ms 17492 KB Output is correct
13 Correct 8 ms 17556 KB Output is correct
14 Correct 8 ms 17492 KB Output is correct
15 Correct 8 ms 17492 KB Output is correct
16 Correct 8 ms 17552 KB Output is correct
17 Correct 7 ms 17492 KB Output is correct
18 Correct 84 ms 25376 KB Output is correct
19 Correct 83 ms 25376 KB Output is correct
20 Correct 84 ms 25292 KB Output is correct
21 Correct 84 ms 25476 KB Output is correct
22 Correct 240 ms 36244 KB Output is correct
23 Correct 325 ms 47856 KB Output is correct
24 Correct 338 ms 48564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 32760 KB Output is correct
2 Correct 188 ms 36452 KB Output is correct
3 Correct 86 ms 25384 KB Output is correct
4 Correct 85 ms 25284 KB Output is correct
5 Correct 384 ms 57464 KB Output is correct
6 Correct 466 ms 58956 KB Output is correct
7 Correct 9 ms 17492 KB Output is correct
8 Correct 338 ms 41304 KB Output is correct
9 Correct 363 ms 47784 KB Output is correct
10 Correct 160 ms 34112 KB Output is correct
11 Correct 175 ms 36468 KB Output is correct
12 Correct 9 ms 17576 KB Output is correct
13 Correct 8 ms 17492 KB Output is correct
14 Correct 8 ms 17492 KB Output is correct
15 Correct 9 ms 17492 KB Output is correct
16 Correct 86 ms 25376 KB Output is correct
17 Correct 88 ms 25432 KB Output is correct
18 Correct 161 ms 33964 KB Output is correct
19 Correct 182 ms 36520 KB Output is correct
20 Correct 165 ms 34132 KB Output is correct
21 Correct 219 ms 35872 KB Output is correct
22 Correct 166 ms 34036 KB Output is correct
23 Correct 184 ms 35888 KB Output is correct
24 Correct 86 ms 25292 KB Output is correct
25 Correct 87 ms 25344 KB Output is correct
26 Correct 127 ms 29324 KB Output is correct
27 Correct 112 ms 28568 KB Output is correct
28 Correct 159 ms 33944 KB Output is correct
29 Correct 153 ms 33296 KB Output is correct
30 Correct 152 ms 33992 KB Output is correct
31 Correct 151 ms 33984 KB Output is correct
32 Correct 9 ms 17552 KB Output is correct
33 Correct 9 ms 17552 KB Output is correct
34 Correct 8 ms 17492 KB Output is correct
35 Correct 8 ms 17492 KB Output is correct
36 Correct 8 ms 17548 KB Output is correct
37 Correct 9 ms 17560 KB Output is correct
38 Correct 9 ms 17548 KB Output is correct
39 Correct 8 ms 17492 KB Output is correct
40 Correct 9 ms 17564 KB Output is correct
41 Correct 10 ms 17876 KB Output is correct
42 Correct 9 ms 17556 KB Output is correct
43 Correct 10 ms 17692 KB Output is correct
44 Correct 9 ms 17552 KB Output is correct
45 Correct 9 ms 17564 KB Output is correct
46 Correct 9 ms 17620 KB Output is correct
47 Correct 11 ms 17748 KB Output is correct
48 Correct 91 ms 21068 KB Output is correct
49 Correct 86 ms 21836 KB Output is correct
50 Correct 69 ms 21836 KB Output is correct
51 Correct 70 ms 21912 KB Output is correct
52 Correct 76 ms 21836 KB Output is correct
53 Correct 149 ms 25936 KB Output is correct
54 Correct 31 ms 18548 KB Output is correct
55 Correct 79 ms 20256 KB Output is correct
56 Correct 12 ms 17620 KB Output is correct
57 Correct 27 ms 18436 KB Output is correct
58 Correct 12 ms 18004 KB Output is correct
59 Correct 419 ms 35864 KB Output is correct
60 Correct 581 ms 42448 KB Output is correct
61 Correct 744 ms 43492 KB Output is correct
62 Correct 711 ms 43356 KB Output is correct
63 Correct 502 ms 44172 KB Output is correct
64 Correct 742 ms 43392 KB Output is correct
65 Correct 717 ms 43352 KB Output is correct
66 Correct 277 ms 28620 KB Output is correct
67 Correct 762 ms 44000 KB Output is correct
68 Correct 184 ms 33692 KB Output is correct
69 Correct 130 ms 32228 KB Output is correct
70 Correct 257 ms 46884 KB Output is correct
71 Correct 8 ms 17492 KB Output is correct
72 Correct 8 ms 17556 KB Output is correct
73 Correct 8 ms 17492 KB Output is correct
74 Correct 8 ms 17492 KB Output is correct
75 Correct 8 ms 17552 KB Output is correct
76 Correct 7 ms 17492 KB Output is correct
77 Correct 84 ms 25376 KB Output is correct
78 Correct 83 ms 25376 KB Output is correct
79 Correct 84 ms 25292 KB Output is correct
80 Correct 84 ms 25476 KB Output is correct
81 Correct 240 ms 36244 KB Output is correct
82 Correct 325 ms 47856 KB Output is correct
83 Correct 338 ms 48564 KB Output is correct
84 Correct 861 ms 53652 KB Output is correct
85 Correct 903 ms 57004 KB Output is correct
86 Correct 645 ms 57036 KB Output is correct
87 Correct 675 ms 57804 KB Output is correct
88 Correct 8 ms 17492 KB Output is correct
89 Correct 660 ms 57908 KB Output is correct
90 Correct 699 ms 56656 KB Output is correct
91 Correct 482 ms 55372 KB Output is correct