답안 #651040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651040 2022-10-16T14:40:04 Z NaimSS 메기 농장 (IOI22_fish) C++17
100 / 100
424 ms 114340 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define pb push_back
const int N = 100100;
typedef long long ll;
typedef pair<ll,ll> pll;
vector<pll> dp[N][2];
vector<pll> suf[N];
vector<pll> pref[N];

vector<int> posi[N];
vector<int> ys[N];
vector<pll> peixes[N];
vector<pll> peixesSum[N];
const ll inf = 1e18;
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,std::vector<int> W) {
  for(auto &it : Y)it++;
  rep(i,0,M){
    peixes[X[i]].pb(pll(Y[i],W[i]));
    ys[X[i]].pb(Y[i]);
  }

  for(int i=0;i<N;i++){
    sort(all(peixes[i]));
    rep(j,0,sz(peixes[i])){
      peixesSum[i].pb(peixes[i][j]);
      if(j)peixesSum[i][j].ss+=peixesSum[i][j-1].ss;
    }
    posi[i].pb(0);
    if(i)for(auto it : ys[i-1]){
      posi[i].pb(it);
    }
    if(i+1<N)for(auto it : ys[i+1]){
      posi[i].pb(it);
    }
    Unique(posi[i]);
  }



  for(int i=0;i<N;i++){
    
    // calc my dp

    for(int j=0;j<2;j++){

      if(j == 0){
        // subindo!
        // h(i-1) <= h(i).
        // pego um prefixo + valores dos peixes do anterior
        ll val=0;
        int pt=0;
        int ptPref=0;
        for(auto it : posi[i]){
          if(i == 0){
            dp[i][j].pb(pll(it,0));
            continue;
          }
          while(pt < sz(peixes[i-1]) && peixes[i-1][pt].ff<=it){
            val+=peixes[i-1][pt].ss;
            pt++;
          }
          while(ptPref + 1 < sz(pref[i-1]) && pref[i-1][ptPref+1].ff <= it)
            ptPref++;
          dp[i][j].pb(pll(it,pref[i-1][ptPref].ss + val));

        }

      }else{
        // descendo
        // h(i-1) >= h[i]
        // pega sufixo - valores dos meus peixes
        ll val=0;
        int pt=0;
        for(auto it : posi[i]){
          if(i == 0){
            dp[i][j].pb(pll(it,0));
            continue;
          }
          while(pt < sz(peixes[i]) && peixes[i][pt].ff<=it){
            val-=peixes[i][pt].ss;
            pt++;
          }
          auto ItSuf = lower_bound(all(suf[i-1]),pll(it,-inf));
          if(ItSuf == suf[i-1].end()){
            dp[i][j].pb(pll(it,0));
          }else{
            dp[i][j].pb(pll(it,val + ItSuf->ss));
          }
        }
      }

    }
    // calc pref -> subtrair os peixes de i

    ll val=0;
    int pt=0;
    for(auto it : dp[i][0]){
      while(pt < sz(peixes[i]) && peixes[i][pt].ff <= it.ff){
        val+=peixes[i][pt].ss;
        pt++;
      }
      pref[i].pb(pll(it.ff,it.ss - val));
    }
    // pref[i][0]: pode ter ignorado tudo tb:
    if(i){
      rep(j,0,sz(dp[i-1][0])){
        ll v = max(dp[i-1][0][j].ss,dp[i-1][1][j].ss);
        pref[i][0].ss = max(pref[i][0].ss,v);
      }
    }

    for(int j=1;j<sz(pref[i]);j++){
      pref[i][j].ss = max(pref[i][j].ss,pref[i][j-1].ss);
    }

    // calc suf -> somar peixes de i+1
    int K = 0;
    for(auto it : dp[i][1]){
      ll peixesNext = 0;
      if(i+1<N){
        int pos = upper_bound(all(peixes[i+1]),pll(it.ff,inf)) - peixes[i+1].begin() - 1;
        if(pos >= 0)peixesNext = peixesSum[i+1][pos].ss;
      }
      suf[i].pb(pll(it.ff,max(it.ss,dp[i][0][K].ss) + peixesNext));
      K++;
    }

    for(int j=sz(suf[i])-2;j>=0;j--){
      suf[i][j].ss = max(suf[i][j].ss,suf[i][j+1].ss);
    }

  }
  
  ll res=0;
  rep(j,0,2)for(auto it : dp[N-1][j])res=max(res,it.ss);
  return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 43524 KB Output is correct
2 Correct 104 ms 47400 KB Output is correct
3 Correct 39 ms 34688 KB Output is correct
4 Correct 40 ms 34636 KB Output is correct
5 Correct 285 ms 94364 KB Output is correct
6 Correct 382 ms 113356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19056 KB Output is correct
2 Correct 160 ms 59728 KB Output is correct
3 Correct 184 ms 66800 KB Output is correct
4 Correct 85 ms 43460 KB Output is correct
5 Correct 102 ms 47424 KB Output is correct
6 Correct 11 ms 18988 KB Output is correct
7 Correct 11 ms 19028 KB Output is correct
8 Correct 12 ms 19076 KB Output is correct
9 Correct 11 ms 19028 KB Output is correct
10 Correct 42 ms 34716 KB Output is correct
11 Correct 41 ms 34632 KB Output is correct
12 Correct 112 ms 48992 KB Output is correct
13 Correct 116 ms 54048 KB Output is correct
14 Correct 91 ms 46384 KB Output is correct
15 Correct 107 ms 49460 KB Output is correct
16 Correct 93 ms 46376 KB Output is correct
17 Correct 105 ms 49216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 34636 KB Output is correct
2 Correct 40 ms 34732 KB Output is correct
3 Correct 93 ms 43028 KB Output is correct
4 Correct 79 ms 41420 KB Output is correct
5 Correct 136 ms 52696 KB Output is correct
6 Correct 148 ms 52724 KB Output is correct
7 Correct 136 ms 52708 KB Output is correct
8 Correct 145 ms 52676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19068 KB Output is correct
2 Correct 11 ms 19028 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 11 ms 19028 KB Output is correct
5 Correct 13 ms 19012 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 11 ms 19028 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19572 KB Output is correct
11 Correct 11 ms 19284 KB Output is correct
12 Correct 12 ms 19304 KB Output is correct
13 Correct 11 ms 19136 KB Output is correct
14 Correct 12 ms 19284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19068 KB Output is correct
2 Correct 11 ms 19028 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 11 ms 19028 KB Output is correct
5 Correct 13 ms 19012 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 11 ms 19028 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19572 KB Output is correct
11 Correct 11 ms 19284 KB Output is correct
12 Correct 12 ms 19304 KB Output is correct
13 Correct 11 ms 19136 KB Output is correct
14 Correct 12 ms 19284 KB Output is correct
15 Correct 10 ms 19156 KB Output is correct
16 Correct 12 ms 19512 KB Output is correct
17 Correct 40 ms 28424 KB Output is correct
18 Correct 39 ms 29420 KB Output is correct
19 Correct 41 ms 29364 KB Output is correct
20 Correct 45 ms 28572 KB Output is correct
21 Correct 36 ms 28492 KB Output is correct
22 Correct 62 ms 37704 KB Output is correct
23 Correct 19 ms 21332 KB Output is correct
24 Correct 33 ms 26308 KB Output is correct
25 Correct 12 ms 19412 KB Output is correct
26 Correct 18 ms 20996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19068 KB Output is correct
2 Correct 11 ms 19028 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 11 ms 19028 KB Output is correct
5 Correct 13 ms 19012 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 11 ms 19028 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19572 KB Output is correct
11 Correct 11 ms 19284 KB Output is correct
12 Correct 12 ms 19304 KB Output is correct
13 Correct 11 ms 19136 KB Output is correct
14 Correct 12 ms 19284 KB Output is correct
15 Correct 10 ms 19156 KB Output is correct
16 Correct 12 ms 19512 KB Output is correct
17 Correct 40 ms 28424 KB Output is correct
18 Correct 39 ms 29420 KB Output is correct
19 Correct 41 ms 29364 KB Output is correct
20 Correct 45 ms 28572 KB Output is correct
21 Correct 36 ms 28492 KB Output is correct
22 Correct 62 ms 37704 KB Output is correct
23 Correct 19 ms 21332 KB Output is correct
24 Correct 33 ms 26308 KB Output is correct
25 Correct 12 ms 19412 KB Output is correct
26 Correct 18 ms 20996 KB Output is correct
27 Correct 13 ms 20400 KB Output is correct
28 Correct 156 ms 61456 KB Output is correct
29 Correct 226 ms 75968 KB Output is correct
30 Correct 235 ms 91736 KB Output is correct
31 Correct 231 ms 91876 KB Output is correct
32 Correct 208 ms 75512 KB Output is correct
33 Correct 241 ms 91752 KB Output is correct
34 Correct 246 ms 91740 KB Output is correct
35 Correct 103 ms 48684 KB Output is correct
36 Correct 247 ms 85736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 34636 KB Output is correct
2 Correct 40 ms 34732 KB Output is correct
3 Correct 93 ms 43028 KB Output is correct
4 Correct 79 ms 41420 KB Output is correct
5 Correct 136 ms 52696 KB Output is correct
6 Correct 148 ms 52724 KB Output is correct
7 Correct 136 ms 52708 KB Output is correct
8 Correct 145 ms 52676 KB Output is correct
9 Correct 173 ms 65228 KB Output is correct
10 Correct 109 ms 46124 KB Output is correct
11 Correct 239 ms 73040 KB Output is correct
12 Correct 11 ms 19028 KB Output is correct
13 Correct 14 ms 19092 KB Output is correct
14 Correct 10 ms 19080 KB Output is correct
15 Correct 11 ms 19028 KB Output is correct
16 Correct 11 ms 19028 KB Output is correct
17 Correct 11 ms 19012 KB Output is correct
18 Correct 44 ms 34644 KB Output is correct
19 Correct 42 ms 34636 KB Output is correct
20 Correct 42 ms 34704 KB Output is correct
21 Correct 43 ms 34740 KB Output is correct
22 Correct 188 ms 61236 KB Output is correct
23 Correct 291 ms 87244 KB Output is correct
24 Correct 292 ms 91876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 43524 KB Output is correct
2 Correct 104 ms 47400 KB Output is correct
3 Correct 39 ms 34688 KB Output is correct
4 Correct 40 ms 34636 KB Output is correct
5 Correct 285 ms 94364 KB Output is correct
6 Correct 382 ms 113356 KB Output is correct
7 Correct 10 ms 19056 KB Output is correct
8 Correct 160 ms 59728 KB Output is correct
9 Correct 184 ms 66800 KB Output is correct
10 Correct 85 ms 43460 KB Output is correct
11 Correct 102 ms 47424 KB Output is correct
12 Correct 11 ms 18988 KB Output is correct
13 Correct 11 ms 19028 KB Output is correct
14 Correct 12 ms 19076 KB Output is correct
15 Correct 11 ms 19028 KB Output is correct
16 Correct 42 ms 34716 KB Output is correct
17 Correct 41 ms 34632 KB Output is correct
18 Correct 112 ms 48992 KB Output is correct
19 Correct 116 ms 54048 KB Output is correct
20 Correct 91 ms 46384 KB Output is correct
21 Correct 107 ms 49460 KB Output is correct
22 Correct 93 ms 46376 KB Output is correct
23 Correct 105 ms 49216 KB Output is correct
24 Correct 40 ms 34636 KB Output is correct
25 Correct 40 ms 34732 KB Output is correct
26 Correct 93 ms 43028 KB Output is correct
27 Correct 79 ms 41420 KB Output is correct
28 Correct 136 ms 52696 KB Output is correct
29 Correct 148 ms 52724 KB Output is correct
30 Correct 136 ms 52708 KB Output is correct
31 Correct 145 ms 52676 KB Output is correct
32 Correct 12 ms 19068 KB Output is correct
33 Correct 11 ms 19028 KB Output is correct
34 Correct 11 ms 19028 KB Output is correct
35 Correct 11 ms 19028 KB Output is correct
36 Correct 13 ms 19012 KB Output is correct
37 Correct 10 ms 19028 KB Output is correct
38 Correct 10 ms 19028 KB Output is correct
39 Correct 11 ms 19028 KB Output is correct
40 Correct 11 ms 19156 KB Output is correct
41 Correct 12 ms 19572 KB Output is correct
42 Correct 11 ms 19284 KB Output is correct
43 Correct 12 ms 19304 KB Output is correct
44 Correct 11 ms 19136 KB Output is correct
45 Correct 12 ms 19284 KB Output is correct
46 Correct 10 ms 19156 KB Output is correct
47 Correct 12 ms 19512 KB Output is correct
48 Correct 40 ms 28424 KB Output is correct
49 Correct 39 ms 29420 KB Output is correct
50 Correct 41 ms 29364 KB Output is correct
51 Correct 45 ms 28572 KB Output is correct
52 Correct 36 ms 28492 KB Output is correct
53 Correct 62 ms 37704 KB Output is correct
54 Correct 19 ms 21332 KB Output is correct
55 Correct 33 ms 26308 KB Output is correct
56 Correct 12 ms 19412 KB Output is correct
57 Correct 18 ms 20996 KB Output is correct
58 Correct 13 ms 20400 KB Output is correct
59 Correct 156 ms 61456 KB Output is correct
60 Correct 226 ms 75968 KB Output is correct
61 Correct 235 ms 91736 KB Output is correct
62 Correct 231 ms 91876 KB Output is correct
63 Correct 208 ms 75512 KB Output is correct
64 Correct 241 ms 91752 KB Output is correct
65 Correct 246 ms 91740 KB Output is correct
66 Correct 103 ms 48684 KB Output is correct
67 Correct 247 ms 85736 KB Output is correct
68 Correct 173 ms 65228 KB Output is correct
69 Correct 109 ms 46124 KB Output is correct
70 Correct 239 ms 73040 KB Output is correct
71 Correct 11 ms 19028 KB Output is correct
72 Correct 14 ms 19092 KB Output is correct
73 Correct 10 ms 19080 KB Output is correct
74 Correct 11 ms 19028 KB Output is correct
75 Correct 11 ms 19028 KB Output is correct
76 Correct 11 ms 19012 KB Output is correct
77 Correct 44 ms 34644 KB Output is correct
78 Correct 42 ms 34636 KB Output is correct
79 Correct 42 ms 34704 KB Output is correct
80 Correct 43 ms 34740 KB Output is correct
81 Correct 188 ms 61236 KB Output is correct
82 Correct 291 ms 87244 KB Output is correct
83 Correct 292 ms 91876 KB Output is correct
84 Correct 292 ms 91552 KB Output is correct
85 Correct 325 ms 92680 KB Output is correct
86 Correct 420 ms 112344 KB Output is correct
87 Correct 424 ms 114340 KB Output is correct
88 Correct 10 ms 19028 KB Output is correct
89 Correct 402 ms 114252 KB Output is correct
90 Correct 337 ms 107620 KB Output is correct
91 Correct 260 ms 95720 KB Output is correct